Intro

In this script, I load exchange data from datras and calculate catch of cod and flounder in unit kg/km^2 (with TVL gear), by correcting for gear dimensions, sweeplength and trawl speed, following Orio et al 2017. I also add oxygen, temperature and depth covariates, which I use for modelling this biomass index. I do this for Q1 and Q4. I use Q4 for the condition data. For the condition data, I also need the environmental covariates. Hence, the study is limited to 2019. For other analyses, I need Q1, and don’t need all envi. covariates, and I have stomach data incl. 2020, hence I need to standardize all trawl data available from Datras.

Load libraries

library(tidyverse)
#> Warning: package 'tidyr' was built under R version 4.0.5
library(readxl)
library(tidylog)
library(RCurl)
library(viridis)
library(RColorBrewer)
#> Warning: package 'RColorBrewer' was built under R version 4.0.5
library(patchwork)
library(janitor)
library(icesDatras)
library(mapdata)
library(patchwork)
library(rgdal)
library(raster)
library(sf)
#> Warning: package 'sf' was built under R version 4.0.5
library(rgeos)
library(chron)
library(lattice)
library(ncdf4)
library(marmap)
library(rnaturalearth)
library(rnaturalearthdata)
library(mapplots)
library(geosphere)
#remotes::install_github("pbs-assess/sdmTMB")
library(sdmTMB)
#library(gganimate)

world <- ne_countries(scale = "medium", returnclass = "sf")

For maps

# Specify map ranges
ymin = 54; ymax = 58; xmin = 12; xmax = 22

map_data <- rnaturalearth::ne_countries(
  scale = "medium",
  returnclass = "sf", continent = "europe")

# Crop the polygon for plotting and efficiency:
# st_bbox(map_data) # find the rough coordinates
sf::sf_use_s2(FALSE)
swe_coast <- suppressWarnings(suppressMessages(
  st_crop(map_data,
          c(xmin = xmin, ymin = ymin, xmax = xmax, ymax = ymax))))

# Transform our map into UTM 33 coordinates, which is the equal-area projection we fit in:
utm_zone33 <- 32633
swe_coast_proj <- sf::st_transform(swe_coast, crs = utm_zone33)

#ggplot(swe_coast_proj) + geom_sf() 

# Define plotting theme for main plot
theme_plot <- function(base_size = 10, base_family = "") {
  theme_light(base_size = 10, base_family = "") +
    theme(
      axis.text.x = element_text(angle = 90),
      axis.text = element_text(size = 8),
      legend.text = element_text(size = 8),
      legend.title = element_text(size = 8),
      legend.position = "bottom",
      legend.key.height = unit(0.2, "cm"),
      legend.margin = margin(0, 0, 0, 0),
      legend.box.margin = margin(-5, -5, -5, -5),
      strip.text = element_text(size = 8, colour = 'gray10', margin = margin(b = 1, t = 1)),
      strip.background = element_rect(fill = "grey95")
      )
}

Read data

# Data were read in from getDATRAS on 2021.10.13
# Read HH data
# bits_hh <- getDATRAS(record = "HH", survey = "BITS", years = 1991:2020, quarters = c(1, 4))
# write.csv(bits_hh, "data/DATRAS_exchange/bits_hh.csv")
bits_hh <- read.csv("data/DATRAS_exchange/bits_hh.csv")

# Read HL data
# bits_hl <- getDATRAS(record = "HL", survey = "BITS", years = 1991:2020, quarters = c(1, 4))
# write.csv(bits_hl, "data/DATRAS_exchange/bits_hl.csv")
bits_hl <- read.csv("data/DATRAS_exchange/bits_hl.csv")

# Read CA data
# bits_ca <- getDATRAS(record = "CA", survey = "BITS", years = 1991:2020, quarters = c(1, 4))
# write.csv(bits_ca, "data/DATRAS_exchange/bits_ca.csv")
bits_ca <- read.csv("data/DATRAS_exchange/bits_ca.csv")

# Read gear standardization data 
#sweep <- read.csv("data/from_ale/sweep_9116.csv", sep = ";", dec = ",", fileEncoding = "latin1")
sweep <- read.csv("data/from_ale/sweep_9118_ml.csv", sep = ";", fileEncoding = "latin1")

Standardize catch data

Standardize ships

# Before creating a a new ID, make sure that countries and ships names use the same format
sort(unique(sweep$Ship))
#>  [1] "26HF" "ATL"  "ATLD" "BAL"  "BALL" "BPE"  "CEV"  "CLP"  "CLV"  "COML"
#> [11] "DAN2" "DANS" "DAR"  "GDY"  "HAF"  "KOH"  "KOOT" "MON"  "MONL" "SOL" 
#> [21] "SOL2" "VSH"  "ZBA"
sort(unique(bits_hh$Ship))
#>  [1] "06JR" "06S1" "06SL" "26D4" "26HF" "26HI" "67BC" "77AR" "77MA" "77SE"
#> [11] "90MX" "AA36" "ESLF" "ESOR" "ESTM" "LAIZ" "LTDA" "RUEK" "RUJB" "RUNT"
#> [21] "RUS6"
sort(unique(bits_hl$Ship))
#>  [1] "06JR" "06S1" "06SL" "26D4" "26HF" "26HI" "67BC" "77AR" "77MA" "77SE"
#> [11] "90MX" "AA36" "ESLF" "ESOR" "ESTM" "LAIZ" "LTDA" "RUEK" "RUJB" "RUNT"
#> [21] "RUS6"

# Change back to the old Ship name standard...
# https://vocab.ices.dk/?ref=315
# https://vocab.ices.dk/?ref=315
# Assumptions:
# SOL is Solea on ICES links above, and SOL1 is the older one of the two SOLs (1 and 2)
# DAN is Dana
# sweep %>% filter(Ship == "DANS") %>% distinct(Year, Country)
# sweep %>% filter(Ship == "DAN2") %>% distinct(Year)
# bits_hh %>% filter(Ship == "67BC") %>% distinct(Year, Country)
# sweep %>% filter(Ship == "DAN2") %>% distinct(Year)
# bits_hh %>% filter(Ship == "26D4") %>% distinct(Year) # Strange that 26DF doesn't extend far back. Which ship did the Danes use? Ok, I have no Danish data that old.
# bits_hh %>% filter(Country == "DK") %>% distinct(Year)

bits_hh <- bits_hh %>%
  mutate(Ship2 = fct_recode(Ship,
                            "SOL" = "06S1", 
                            "SOL2" = "06SL",
                            "DAN2" = "26D4",
                            "HAF" = "26HF",
                            "HAF" = "26HI",
                            "HAF" = "67BC",
                            "BAL" = "67BC",
                            "ARG" = "77AR",
                            "77SE" = "77SE",
                            "AA36" = "AA36",
                            "KOOT" = "ESLF",
                            "KOH" = "ESTM",
                            "DAR" = "LTDA",
                            "ATLD" = "RUJB",
                            "ATL" = "RUNT"), 
         Ship2 = as.character(Ship2)) %>% 
  mutate(Ship3 = ifelse(Country == "LV" & Ship2 == "BAL", "BALL", Ship2))

bits_hl <- bits_hl %>%
  mutate(Ship2 = fct_recode(Ship,
                            "SOL" = "06S1", 
                            "SOL2" = "06SL",
                            "DAN2" = "26D4",
                            "HAF" = "26HF",
                            "HAF" = "26HI",
                            "HAF" = "67BC",
                            "BAL" = "67BC",
                            "ARG" = "77AR",
                            "77SE" = "77SE",
                            "AA36" = "AA36",
                            "KOOT" = "ESLF",
                            "KOH" = "ESTM",
                            "DAR" = "LTDA",
                            "ATLD" = "RUJB",
                            "ATL" = "RUNT"), 
         Ship2 = as.character(Ship2)) %>% 
  mutate(Ship3 = ifelse(Country == "LV" & Ship2 == "BAL", "BALL", Ship2))

bits_ca <- bits_ca %>%
  mutate(Ship2 = fct_recode(Ship,
                            "SOL" = "06S1", 
                            "SOL2" = "06SL",
                            "DAN2" = "26D4",
                            "HAF" = "26HF",
                            "HAF" = "26HI",
                            "HAF" = "67BC",
                            "BAL" = "67BC",
                            "ARG" = "77AR",
                            "77SE" = "77SE",
                            "AA36" = "AA36",
                            "KOOT" = "ESLF",
                            "KOH" = "ESTM",
                            "DAR" = "LTDA",
                            "ATLD" = "RUJB",
                            "ATL" = "RUNT"), 
         Ship2 = as.character(Ship2)) %>% 
  mutate(Ship3 = ifelse(Country == "LV" & Ship2 == "BAL", "BALL", Ship2))

# Ok, which ships are missing in the exchange data?
unique(bits_hh$Ship3)[!unique(bits_hh$Ship3) %in% unique(sweep$Ship)]
#>  [1] "AA36" "90MX" "ARG"  "06JR" "LAIZ" "RUEK" "RUS6" "77MA" "77SE" "ESOR"
# Swedish Ships and unidentified ships are NOT in the Sweep data
unique(sweep$Ship3)[!unique(sweep$Ship3) %in% unique(bits_hh$Ship3)]
#> NULL
# But all Sweep Ships are in the exchange data

Standardize countries

# Now check which country codes are used
sort(unique(sweep$Country))
#> [1] "DEN" "EST" "GFR" "LAT" "LTU" "POL" "RUS" "SWE"
sort(unique(bits_hh$Country))
#> [1] "DE" "DK" "EE" "LT" "LV" "PL" "RU" "SE"

# https://www.nationsonline.org/oneworld/country_code_list.htm#E
bits_hh <- bits_hh %>%
  mutate(Country = fct_recode(Country,
                              "DEN" = "DK",
                              "EST" = "EE",
                              "GFR" = "DE",
                              "LAT" = "LV",
                              "LTU" = "LT",
                              "POL" = "PL",
                              "RUS" = "RU",
                              "SWE" = "SE"),
         Country = as.character(Country))

bits_hl <- bits_hl %>%
  mutate(Country = fct_recode(Country,
                              "DEN" = "DK",
                              "EST" = "EE",
                              "GFR" = "DE",
                              "LAT" = "LV",
                              "LTU" = "LT",
                              "POL" = "PL",
                              "RUS" = "RU",
                              "SWE" = "SE"),
         Country = as.character(Country))

bits_ca <- bits_ca %>%
  mutate(Country = fct_recode(Country,
                              "DEN" = "DK",
                              "EST" = "EE",
                              "GFR" = "DE",
                              "LAT" = "LV",
                              "LTU" = "LT",
                              "POL" = "PL",
                              "RUS" = "RU",
                              "SWE" = "SE"),
         Country = as.character(Country))

# Gear? Are they the same?
sort(unique(bits_hh$Gear))
#>  [1] "CAM" "CHP" "DT"  "EGY" "ESB" "EXP" "FOT" "GOV" "GRT" "H20" "HAK" "LBT"
#> [13] "P20" "PEL" "SON" "TVL" "TVS"
sort(unique(bits_hl$Gear))
#>  [1] "CAM" "CHP" "DT"  "EGY" "ESB" "EXP" "FOT" "GOV" "GRT" "H20" "HAK" "LBT"
#> [13] "P20" "PEL" "SON" "TVL" "TVS"
sort(unique(sweep$Gear))
#>  [1] "CAM" "CHP" "DT"  "EGY" "ESB" "EXP" "GRT" "H20" "HAK" "LBT" "LPT" "P20"
#> [13] "PEL" "SON" "TVL" "TVS"

# Which gears are NOT in the sweep data?
unique(bits_hl$Gear)[!unique(bits_hl$Gear) %in% unique(sweep$Gear)] 
#> [1] "FOT" "GOV"

Create a simple haul ID that works across all exchange data

# Create ID column
bits_ca <- bits_ca %>% 
  mutate(IDx = paste(Year, Quarter, Country, Ship, Gear, StNo, HaulNo, sep = "."))

bits_hl <- bits_hl %>% 
  mutate(IDx = paste(Year, Quarter, Country, Ship, Gear, StNo, HaulNo, sep = "."))

bits_hh <- bits_hh %>% 
  mutate(IDx = paste(Year, Quarter, Country, Ship, Gear, StNo, HaulNo, sep = "."))

# Works like a haul-id
bits_hh %>% group_by(IDx) %>% mutate(n = n()) %>% ungroup() %>% distinct(n)
#> # A tibble: 1 × 1
#>       n
#>   <int>
#> 1     1

Create the same unique haul-ID in the cpue data that I have in the sweep-file

bits_hl <- bits_hl %>% 
  mutate(haul.id = paste(Year, Quarter, Country, Ship3, Gear, StNo, HaulNo, sep = ":")) 

bits_hh <- bits_hh %>% 
  mutate(haul.id = paste(Year, Quarter, Country, Ship3, Gear, StNo, HaulNo, sep = ":")) 

bits_hh %>% group_by(haul.id) %>% mutate(n = n()) %>% ungroup() %>% distinct(n)
#> # A tibble: 1 × 1
#>       n
#>   <int>
#> 1     1

Clean DATRAS EXCHANGE data

# Select just valid, additional and no oxygen hauls
bits_hh <- bits_hh %>%
  #filter(!Country == "SWE") %>% # I'll deal with Sweden later...
  filter(HaulVal %in% c("A","N","V"))

# Add ICES rectangle
bits_hh$Rect <- mapplots::ices.rect2(lon = bits_hh$ShootLong, lat = bits_hh$ShootLat)

# Add ICES subdivisions
shape <- shapefile("data/ICES_StatRec_mapto_ICES_Areas/StatRec_map_Areas_Full_20170124.shp")

pts <- SpatialPoints(cbind(bits_hh$ShootLong, bits_hh$ShootLat), 
                     proj4string = CRS(proj4string(shape)))
#> Warning in proj4string(shape): CRS object has comment, which is lost in output

bits_hh$sub_div <- over(pts, shape)$Area_27

# Rename subdivisions to the more common names and do some more filtering (by sub div and area)
sort(unique(bits_hh$sub_div))
#>  [1] "3.a.20"   "3.a.21"   "3.b.23"   "3.c.22"   "3.d.24"   "3.d.25"  
#>  [7] "3.d.26"   "3.d.27"   "3.d.28.1" "3.d.28.2" "3.d.29"

bits_hh <- bits_hh %>% 
  mutate(sub_div = factor(sub_div),
         sub_div = fct_recode(sub_div,
                              "20" = "3.a.20",
                              "21" = "3.a.21",
                              "22" = "3.c.22",
                              "23" = "3.b.23",
                              "24" = "3.d.24",
                              "25" = "3.d.25",
                              "26" = "3.d.26",
                              "27" = "3.d.27",
                              "28" = "3.d.28.1",
                              "28" = "3.d.28.2",
                              "29" = "3.d.29"),
         sub_div = as.character(sub_div)) 

# Now add the fishing line information from the sweep file (we need that later
# to standardize based on gear geometry). We add in the the HH data and then
# transfer it to the other exchange data files when left_joining.
# Check which Fishing lines I have in the sweep data:
fishing_line <- sweep %>% group_by(Gear) %>% distinct(Fishing.line)

bits_hh <- left_join(bits_hh, fishing_line)
# sweep %>% group_by(Gear) %>% distinct(Fishing.line)
# bits_hh %>% group_by(Gear) %>% distinct(Fishing.line)
bits_hh$Fishing.line <- as.numeric(bits_hh$Fishing.line)

# Which gears do now have fishing line?
bits_hh$Fishing.line[is.na(bits_hh$Fishing.line)] <- -9
bits_hh %>% filter(Fishing.line == -9) %>% distinct(Gear)
#>   Gear
#> 1  GRT
#> 2  FOT
#> 3  GOV
#> 4  EXP
#> 5  CAM
#> 6  EGY
#> 7   DT
#> 8  ESB
#> 9  HAK
# 1  GRT
# 2  CAM
# 3  EXP
# 4  FOT
# 5  GOV
# 6  EGY
# 7   DT
# 8  ESB
# 9  HAK

# FROM the index files (Orio, "Research Östersjön 2")
# FOT has 83
# GOV has 160
# ESB ??
# GRT ??
# Rest are unknown and likely not used by Swedish data (therefore their correction
# factors my be in the sweep file)

# Add these values:
bits_hh <- bits_hh %>% mutate(Fishing.line = ifelse(Gear == "FOT", 83, Fishing.line))
bits_hh <- bits_hh %>% mutate(Fishing.line = ifelse(Gear == "GOV", 160, Fishing.line))

# Now select the hauls in the HH data when subsetting the HL data
bits_hl <- bits_hl %>%
  filter(haul.id %in% bits_hh$haul.id)

# Match columns from the HH data to the HL and CA data
sort(unique(bits_hh$sub_div))
#>  [1] "20" "21" "22" "23" "24" "25" "26" "27" "28" "29"
sort(colnames(bits_hh))
#>  [1] "BotCurDir"         "BotCurSpeed"       "BotSal"           
#>  [4] "BotTemp"           "Buoyancy"          "BySpecRecCode"    
#>  [7] "CodendMesh"        "Country"           "DataType"         
#> [10] "DateofCalculation" "Day"               "DayNight"         
#> [13] "Depth"             "DepthStratum"      "Distance"         
#> [16] "DoorSpread"        "DoorSurface"       "DoorType"         
#> [19] "DoorWgt"           "Fishing.line"      "Gear"             
#> [22] "GearEx"            "GroundSpeed"       "haul.id"          
#> [25] "HaulDur"           "HaulLat"           "HaulLong"         
#> [28] "HaulNo"            "HaulVal"           "HydroStNo"        
#> [31] "IDx"               "KiteDim"           "MaxTrawlDepth"    
#> [34] "MinTrawlDepth"     "Month"             "Netopening"       
#> [37] "PelSampType"       "Quarter"           "RecordType"       
#> [40] "Rect"              "Rigging"           "SecchiDepth"      
#> [43] "Ship"              "Ship2"             "Ship3"            
#> [46] "ShootLat"          "ShootLong"         "SpeedWater"       
#> [49] "StatRec"           "StdSpecRecCode"    "StNo"             
#> [52] "sub_div"           "SurCurDir"         "SurCurSpeed"      
#> [55] "SurSal"            "SurTemp"           "Survey"           
#> [58] "SweepLngt"         "SwellDir"          "SwellHeight"      
#> [61] "ThClineDepth"      "ThermoCline"       "Tickler"          
#> [64] "TidePhase"         "TideSpeed"         "TimeShot"         
#> [67] "TowDir"            "Turbidity"         "WarpDen"          
#> [70] "Warpdia"           "Warplngt"          "WgtGroundRope"    
#> [73] "WindDir"           "WindSpeed"         "WingSpread"       
#> [76] "X"                 "Year"

bits_hh_merge <- bits_hh %>% 
  dplyr::select(sub_div, Rect, HaulVal, StdSpecRecCode, BySpecRecCode, Fishing.line,
                DataType, HaulDur, GroundSpeed, haul.id, IDx, ShootLat, ShootLong, Depth)

bits_hl <- left_join(dplyr::select(bits_hl, -haul.id), bits_hh_merge, by = "IDx")
bits_ca <- left_join(bits_ca, bits_hh_merge, by = "IDx")

# Now filter the subdivisions I want from all data sets
bits_hh <- bits_hh %>% filter(sub_div %in% c(24, 25, 26, 27, 28))
bits_hl <- bits_hl %>% filter(sub_div %in% c(24, 25, 26, 27, 28))
bits_ca <- bits_ca %>% filter(sub_div %in% c(24, 25, 26, 27, 28))

Filter species

hlcod <- bits_hl %>%
  filter(SpecCode %in% c("126436", "164712")) %>% 
  mutate(Species = "Gadus morhua")

hlfle <- bits_hl %>%
  filter(SpecCode %in% c("127141", "172894")) %>% 
  mutate(Species = "Platichthys flesus")

Prepare to add 0 catches

# Find common columns in the HH and HL data (here already subset by species)
comcol <- intersect(names(hlcod), names(bits_hh))

# What is the proportion of zero-catch hauls?
# Here we don't have zero catches
hlcod %>%
  group_by(haul.id, Year) %>%
  summarise(CPUEun_haul = sum(HLNoAtLngt)) %>% 
  ungroup() %>% 
  mutate(zero_catch = ifelse(CPUEun_haul == 0, "Y", "N")) %>% 
  distinct(zero_catch)
#> # A tibble: 1 × 1
#>   zero_catch
#>   <chr>     
#> 1 N

# Cod: Add 0s and then remove lines with SpecVal = 0 (first NA because we don't have a match in the HH, then make them 0 later)
hlcod0 <- full_join(hlcod, bits_hh[, comcol], by = comcol)

# No zeroes yet
hlcod0 %>%
  group_by(haul.id, Year) %>%
  summarise(CPUEun_haul = sum(HLNoAtLngt)) %>% 
  ungroup() %>% 
  mutate(zero_catch = ifelse(CPUEun_haul == 0, "Y", "N")) %>% 
  distinct(zero_catch) 
#> # A tibble: 1 × 1
#>   zero_catch
#>   <lgl>     
#> 1 NA

hlcod0$SpecVal[is.na(hlcod0$SpecVal)] <- "zeroCatch"

hlcod0$SpecVal <- factor(hlcod0$SpecVal)

hlcod0 <-  hlcod0 %>% filter(!SpecVal == "0")

# Add species again after merge
hlcod0$Species<-"Gadus morhua"

# Flounder: Add 0s, remove them if StdSpecRecCode !=1 and then remove lines with SpecVal = 0
hlfle0 <- full_join(hlfle, bits_hh[, comcol], by = comcol)

hlfle0 <- hlfle0[!(is.na(hlfle0$Species) & hlfle0$StdSpecRecCode != 1),] 

hlfle0$SpecVal[is.na(hlfle0$SpecVal)] <- "zeroCatch"
hlfle0$SpecVal <- factor(hlfle0$SpecVal)

hlfle0 <-  hlfle0 %>% filter(!SpecVal == "0")

hlfle0$Species<-"Platichthys flesus"

# Check number of hauls per species
hlcod0 %>% distinct(haul.id) %>% nrow()
#> [1] 12488
hlfle0 %>% distinct(haul.id) %>% nrow()
#> [1] 12214

Create (unstandardized) CPUE for SpecVal=1. If DataType=C then CPUEun=HLNoAtLngt, if DataType=R then CPUEun=HLNoAtLngt/(HaulDur/60), if DataType=S then CPUEun=(HLNoAtLngt*SubFactor)/(HaulDur/60). If SpecVal="zeroCatch" then CPUEun=0, if SpecVal=4 we need to decide (no length measurements, only total catch). Note that here we also add zero CPUE if SpecVal=="zeroCatch".

Then I will sum for the same haul the CPUE of the same length classes if they were sampled with different subfactors or with different sexes.

# Cod
hlcod0 <- hlcod0 %>%
  mutate(CPUEun = ifelse(SpecVal == "1" & DataType == "C",
                         HLNoAtLngt,
                         
                         ifelse(SpecVal == "1" & DataType == "R",
                                HLNoAtLngt/(HaulDur/60),
                                
                                ifelse(SpecVal == "1" & DataType == "S",
                                       (HLNoAtLngt*SubFactor)/(HaulDur/60),
                                       
                                       ifelse(SpecVal == "zeroCatch", 0, NA)))))

# Plot and fill by zero catch
hlcod0 %>%
  group_by(haul.id, Year) %>%
  summarise(CPUEun_haul = sum(CPUEun)) %>% 
  ungroup() %>% 
  mutate(zero_catch = ifelse(CPUEun_haul == 0, "Y", "N")) %>%
  group_by(Year, zero_catch) %>% 
  summarise(n = n()) %>% 
  ggplot(., aes(x = Year, y = n, fill = zero_catch)) +
  geom_bar(stat = "identity")


# Some rows have multiple rows per combination of length class and haul id,
# so we need to sum it up 
hlcod0 %>% group_by(LngtClass, haul.id) %>% mutate(n = n()) %>% ungroup() %>% distinct(n)
#> # A tibble: 2 × 1
#>       n
#>   <int>
#> 1     1
#> 2     2
hlcod0 %>% group_by(LngtClass, haul.id) %>% mutate(n = n()) %>% ungroup() %>% filter(n == 2) %>% as.data.frame() %>% head(20)
#>         X RecordType Survey Quarter Country Ship Gear SweepLngt GearEx DoorType
#> 1    6847         HL   BITS       1     LAT 90MX  LBT        NA   <NA>       NA
#> 2   13793         HL   BITS       1     LAT 90MX  LBT        NA   <NA>       NA
#> 3   13988         HL   BITS       1     LAT 90MX  LBT        NA   <NA>       NA
#> 4   58045         HL   BITS       1     LAT LAIZ  LBT        NA   <NA>       NA
#> 5  205495         HL   BITS       1     RUS RUJB  HAK        NA   <NA>       NA
#> 6  300422         HL   BITS       4     EST ESLF  TVS        NA   <NA>       NA
#> 7  300423         HL   BITS       4     EST ESLF  TVS        NA   <NA>       NA
#> 8  325369         HL   BITS       4     DEN 26D4  TVL        NA      R       NA
#> 9  325375         HL   BITS       4     DEN 26D4  TVL        NA      R       NA
#> 10 326062         HL   BITS       4     DEN 26D4  TVL        NA      R       NA
#> 11 326064         HL   BITS       4     DEN 26D4  TVL        NA      R       NA
#> 12 326065         HL   BITS       4     DEN 26D4  TVL        NA      R       NA
#> 13 326066         HL   BITS       4     DEN 26D4  TVL        NA      R       NA
#> 14 326067         HL   BITS       4     DEN 26D4  TVL        NA      R       NA
#> 15 326068         HL   BITS       4     DEN 26D4  TVL        NA      R       NA
#> 16 326069         HL   BITS       4     DEN 26D4  TVL        NA      R       NA
#> 17 326071         HL   BITS       4     DEN 26D4  TVL        NA      R       NA
#> 18 326073         HL   BITS       4     DEN 26D4  TVL        NA      R       NA
#> 19 326077         HL   BITS       4     DEN 26D4  TVL        NA      R       NA
#> 20 326079         HL   BITS       4     DEN 26D4  TVL        NA      R       NA
#>      StNo HaulNo Year SpecCodeType SpecCode SpecVal  Sex TotalNo CatIdentifier
#> 1  000001      5 1991            W   126436       4 <NA>       3             1
#> 2  000003     33 1991            W   126436       4 <NA>       6             1
#> 3  000003     29 1991            W   126436       4 <NA>     183             1
#> 4  000001     23 1993            W   126436       4 <NA>       1             1
#> 5    <NA>     10 1998            T   164712       4 <NA>      -9             1
#> 6      14     14 2000            T   164712       1    M       2             1
#> 7      14     14 2000            T   164712       1    F       4             1
#> 8      63     31 2001            T   164712       1    M       5             1
#> 9      63     31 2001            T   164712       1    F       7             1
#> 10     83     37 2001            T   164712       1    M      21             1
#> 11     83     37 2001            T   164712       1    M      21             1
#> 12     83     37 2001            T   164712       1    M      21             1
#> 13     83     37 2001            T   164712       1    M      21             1
#> 14     83     37 2001            T   164712       1    M      21             1
#> 15     83     37 2001            T   164712       1    M      21             1
#> 16     83     37 2001            T   164712       1    M      21             1
#> 17     83     37 2001            T   164712       1    M      21             1
#> 18     83     37 2001            T   164712       1    M      21             1
#> 19     83     37 2001            T   164712       1    F      42             1
#> 20     83     37 2001            T   164712       1    F      42             1
#>    NoMeas SubFactor SubWgt CatCatchWgt LngtCode LngtClass HLNoAtLngt DevStage
#> 1      NA         1     NA          NA     <NA>        NA         -9     <NA>
#> 2      NA         1     NA          NA     <NA>        NA         -9     <NA>
#> 3      NA         1     NA          NA     <NA>        NA         -9     <NA>
#> 4      NA         1     NA          NA     <NA>        NA         -9     <NA>
#> 5      NA         1     NA          NA     <NA>        NA         -9     <NA>
#> 6       3         1     NA          32        1        35          2     <NA>
#> 7       3         1     NA          32        1        35          2     <NA>
#> 8       5         1     NA        3142        1        22          2     <NA>
#> 9       7         1     NA        3142        1        22          1     <NA>
#> 10     21         1     NA       64261        1        22          1     <NA>
#> 11     21         1     NA       64261        1        38          2     <NA>
#> 12     21         1     NA       64261        1        39          2     <NA>
#> 13     21         1     NA       64261        1        41          1     <NA>
#> 14     21         1     NA       64261        1        42          1     <NA>
#> 15     21         1     NA       64261        1        44          2     <NA>
#> 16     21         1     NA       64261        1        45          4     <NA>
#> 17     21         1     NA       64261        1        48          2     <NA>
#> 18     21         1     NA       64261        1        52          1     <NA>
#> 19     42         1     NA       64261        1        22          1     <NA>
#> 20     42         1     NA       64261        1        38          2     <NA>
#>    LenMeasType DateofCalculation Valid_Aphia Ship2 Ship3
#> 1           NA          20211203      126436  90MX  90MX
#> 2           NA          20211203      126436  90MX  90MX
#> 3           NA          20211203      126436  90MX  90MX
#> 4           NA          20211203      126436  LAIZ  LAIZ
#> 5           NA          20140617      126436  ATLD  ATLD
#> 6           NA          20131112      126436  KOOT  KOOT
#> 7           NA          20131112      126436  KOOT  KOOT
#> 8           NA          20131113      126436  DAN2  DAN2
#> 9           NA          20131113      126436  DAN2  DAN2
#> 10          NA          20131113      126436  DAN2  DAN2
#> 11          NA          20131113      126436  DAN2  DAN2
#> 12          NA          20131113      126436  DAN2  DAN2
#> 13          NA          20131113      126436  DAN2  DAN2
#> 14          NA          20131113      126436  DAN2  DAN2
#> 15          NA          20131113      126436  DAN2  DAN2
#> 16          NA          20131113      126436  DAN2  DAN2
#> 17          NA          20131113      126436  DAN2  DAN2
#> 18          NA          20131113      126436  DAN2  DAN2
#> 19          NA          20131113      126436  DAN2  DAN2
#> 20          NA          20131113      126436  DAN2  DAN2
#>                              IDx sub_div Rect HaulVal StdSpecRecCode
#> 1   1991.1.LAT.90MX.LBT.000001.5      28 43H1       V              1
#> 2  1991.1.LAT.90MX.LBT.000003.33      28 43H0       V              1
#> 3  1991.1.LAT.90MX.LBT.000003.29      28 43H0       V              1
#> 4  1993.1.LAT.LAIZ.LBT.000001.23      28 43H1       V              1
#> 5      1998.1.RUS.RUJB.HAK.NA.10      26 38G9       V              1
#> 6      2000.4.EST.ESLF.TVS.14.14      28 45H1       V              1
#> 7      2000.4.EST.ESLF.TVS.14.14      28 45H1       V              1
#> 8      2001.4.DEN.26D4.TVL.63.31      26 39G8       V              1
#> 9      2001.4.DEN.26D4.TVL.63.31      26 39G8       V              1
#> 10     2001.4.DEN.26D4.TVL.83.37      26 40G8       V              1
#> 11     2001.4.DEN.26D4.TVL.83.37      26 40G8       V              1
#> 12     2001.4.DEN.26D4.TVL.83.37      26 40G8       V              1
#> 13     2001.4.DEN.26D4.TVL.83.37      26 40G8       V              1
#> 14     2001.4.DEN.26D4.TVL.83.37      26 40G8       V              1
#> 15     2001.4.DEN.26D4.TVL.83.37      26 40G8       V              1
#> 16     2001.4.DEN.26D4.TVL.83.37      26 40G8       V              1
#> 17     2001.4.DEN.26D4.TVL.83.37      26 40G8       V              1
#> 18     2001.4.DEN.26D4.TVL.83.37      26 40G8       V              1
#> 19     2001.4.DEN.26D4.TVL.83.37      26 40G8       V              1
#> 20     2001.4.DEN.26D4.TVL.83.37      26 40G8       V              1
#>    BySpecRecCode Fishing.line DataType HaulDur GroundSpeed
#> 1              1        28.00        C      40        -9.0
#> 2              1        28.00        C      30        -9.0
#> 3              1        28.00        C      20        -9.0
#> 4              1        28.00        C      60        -9.0
#> 5              1        -9.00        C      30         3.8
#> 6              1        33.22        C      30         3.0
#> 7              1        33.22        C      30         3.0
#> 8              1        63.46        R      30         3.0
#> 9              1        63.46        R      30         3.0
#> 10             1        63.46        R      31         3.1
#> 11             1        63.46        R      31         3.1
#> 12             1        63.46        R      31         3.1
#> 13             1        63.46        R      31         3.1
#> 14             1        63.46        R      31         3.1
#> 15             1        63.46        R      31         3.1
#> 16             1        63.46        R      31         3.1
#> 17             1        63.46        R      31         3.1
#> 18             1        63.46        R      31         3.1
#> 19             1        63.46        R      31         3.1
#> 20             1        63.46        R      31         3.1
#>                          haul.id ShootLat ShootLong Depth      Species   CPUEun
#> 1   1991:1:LAT:90MX:LBT:000001:5  57.3833   21.3000    35 Gadus morhua       NA
#> 2  1991:1:LAT:90MX:LBT:000003:33  57.2167   20.7000    80 Gadus morhua       NA
#> 3  1991:1:LAT:90MX:LBT:000003:29  57.1500   20.6500    93 Gadus morhua       NA
#> 4  1993:1:LAT:LAIZ:LBT:000001:23  57.3000   21.2333    40 Gadus morhua       NA
#> 5      1998:1:RUS:ATLD:HAK:NA:10  54.6333   19.6500    32 Gadus morhua       NA
#> 6      2000:4:EST:KOOT:TVS:14:14  58.0167   21.0833    74 Gadus morhua 2.000000
#> 7      2000:4:EST:KOOT:TVS:14:14  58.0167   21.0833    74 Gadus morhua 2.000000
#> 8      2001:4:DEN:DAN2:TVL:63:31  55.4699   18.3116    88 Gadus morhua 4.000000
#> 9      2001:4:DEN:DAN2:TVL:63:31  55.4699   18.3116    88 Gadus morhua 2.000000
#> 10     2001:4:DEN:DAN2:TVL:83:37  55.6627   18.0414    69 Gadus morhua 1.935484
#> 11     2001:4:DEN:DAN2:TVL:83:37  55.6627   18.0414    69 Gadus morhua 3.870968
#> 12     2001:4:DEN:DAN2:TVL:83:37  55.6627   18.0414    69 Gadus morhua 3.870968
#> 13     2001:4:DEN:DAN2:TVL:83:37  55.6627   18.0414    69 Gadus morhua 1.935484
#> 14     2001:4:DEN:DAN2:TVL:83:37  55.6627   18.0414    69 Gadus morhua 1.935484
#> 15     2001:4:DEN:DAN2:TVL:83:37  55.6627   18.0414    69 Gadus morhua 3.870968
#> 16     2001:4:DEN:DAN2:TVL:83:37  55.6627   18.0414    69 Gadus morhua 7.741935
#> 17     2001:4:DEN:DAN2:TVL:83:37  55.6627   18.0414    69 Gadus morhua 3.870968
#> 18     2001:4:DEN:DAN2:TVL:83:37  55.6627   18.0414    69 Gadus morhua 1.935484
#> 19     2001:4:DEN:DAN2:TVL:83:37  55.6627   18.0414    69 Gadus morhua 1.935484
#> 20     2001:4:DEN:DAN2:TVL:83:37  55.6627   18.0414    69 Gadus morhua 3.870968
#>    n
#> 1  2
#> 2  2
#> 3  2
#> 4  2
#> 5  2
#> 6  2
#> 7  2
#> 8  2
#> 9  2
#> 10 2
#> 11 2
#> 12 2
#> 13 2
#> 14 2
#> 15 2
#> 16 2
#> 17 2
#> 18 2
#> 19 2
#> 20 2
test <- hlcod0 %>% group_by(LngtClass, haul.id) %>% mutate(n = n()) %>% ungroup() %>% filter(n == 2)
test_id <- test$haul.id[2]

hlcodL <- hlcod0 %>% 
  group_by(LngtClass, haul.id) %>% 
  mutate(CPUEun = sum(CPUEun)) %>%
  ungroup() %>% 
  mutate(id3 = paste(haul.id, LngtClass)) %>% 
  distinct(id3, .keep_all = TRUE) %>% 
  dplyr::select(-X, -id3) # Clean up a bit

# Check with an ID
# filter(hlcod0, haul.id == test_id)
# filter(hlcodL, haul.id == test_id) %>% as.data.frame()

# Do we still have 0 catches?
hlcodL %>%
  group_by(haul.id, Year) %>%
  summarise(CPUEun_haul = sum(CPUEun)) %>% 
  ungroup() %>% 
  mutate(zero_catch = ifelse(CPUEun_haul == 0, "Y", "N")) %>%
  group_by(Year, zero_catch) %>% 
  summarise(n = n()) %>% 
  ggplot(., aes(x = Year, y = n, fill = zero_catch)) +
  geom_bar(stat = "identity")


# Flounder
hlfle0 <- hlfle0 %>%
  mutate(CPUEun = ifelse(SpecVal == "1" & DataType == "C",
                         HLNoAtLngt,
                         
                         ifelse(SpecVal == "1" & DataType == "R",
                                HLNoAtLngt/(HaulDur/60),
                                
                                ifelse(SpecVal == "1" & DataType == "S",
                                       (HLNoAtLngt*SubFactor)/(HaulDur/60),
                                       
                                       ifelse(SpecVal == "zeroCatch", 0, NA)))))

# Sum up the CPUES if multiple per length class and haul
hlfleL <- hlfle0 %>% 
  group_by(LngtClass, haul.id) %>% 
  mutate(CPUEun = sum(CPUEun)) %>%
  ungroup() %>% 
  mutate(id3 = paste(haul.id, LngtClass)) %>% 
  distinct(id3, .keep_all = TRUE) %>% 
  dplyr::select(-X, -id3)

Get and add annual weight-length relationships from the CA data for both cod and flounder so that I can calculate CPUE in biomass rather than numbers further down

# Cod
bits_ca_cod <- bits_ca %>% 
  filter(SpecCode %in% c("164712", "126436")) %>% 
  mutate(StNo = as.numeric(StNo)) %>% 
  mutate(Species = "Cod") %>% 
  mutate(ID = paste(Year, Quarter, Country, Ship, Gear, StNo, HaulNo, sep = "."))
#> Warning in mask$eval_all_mutate(quo): NAs introduced by coercion

# Now I need to copy rows with NoAtLngt > 1 so that 1 row = 1 ind
# First make a small test
# nrow(bits_ca_cod)
# test_id <- head(filter(bits_ca_cod, CANoAtLngt == 5))$ID[1]
# filter(bits_ca_cod, ID == test_id & CANoAtLngt == 5)

bits_ca_cod <- bits_ca_cod %>% map_df(., rep, .$CANoAtLngt)

# head(data.frame(filter(bits_ca_cod, ID == test_id & CANoAtLngt == 5)), 20)
# nrow(bits_ca_cod)
# Looks ok!

# Standardize length and drop NA weights (need that for condition)
bits_ca_cod <- bits_ca_cod %>% 
  drop_na(IndWgt) %>% 
  drop_na(LngtClass) %>% 
  filter(IndWgt > 0 & LngtClass > 0) %>%  # Filter positive length and weight
  mutate(weight_kg = IndWgt/1000) %>% 
  mutate(length_cm = ifelse(LngtCode == ".", 
                            LngtClass/10,
                            LngtClass)) # Standardize length ((https://vocab.ices.dk/?ref=18))

# Plot
ggplot(bits_ca_cod, aes(IndWgt, length_cm)) +
  geom_point() + 
  facet_wrap(~Year)


# Now extract the coefficients for each year (not bothering with outliers at the moment)
cod_intercept <- bits_ca_cod %>%
  split(.$Year) %>%
  purrr::map(~lm(log(IndWgt) ~ log(length_cm), data = .x)) %>%
  purrr::map_df(broom::tidy, .id = 'Year') %>%
  filter(term == "(Intercept)") %>% 
  mutate(a = exp(estimate)) %>% 
  mutate(Year = as.integer(Year)) %>% 
  dplyr::select(Year, a, std.error)

cod_slope <- bits_ca_cod %>%
  split(.$Year) %>%
  purrr::map(~lm(log(IndWgt) ~ log(length_cm), data = .x)) %>%
  purrr::map_df(broom::tidy, .id = 'Year') %>%
  filter(term == "log(length_cm)") %>% 
  mutate(Year = as.integer(Year)) %>% 
  rename("b" = "estimate") %>% 
  dplyr::select(Year, b, std.error)

# Flounder
bits_ca_fle <- bits_ca %>% 
  filter(SpecCode %in% c("127141", "172894")) %>% 
  mutate(StNo = as.numeric(StNo)) %>% 
  mutate(Species = "Flounder") %>% 
  mutate(ID = paste(Year, Quarter, Country, Ship, Gear, StNo, HaulNo, sep = "."))
#> Warning in mask$eval_all_mutate(quo): NAs introduced by coercion

bits_ca_fle <- bits_ca_fle %>% map_df(., rep, .$CANoAtLngt)

# Standardize length and drop NA weights (need that for condition)
bits_ca_fle <- bits_ca_fle %>% 
  drop_na(IndWgt) %>% 
  drop_na(LngtClass) %>% 
  filter(IndWgt > 0 & LngtClass > 0) %>%  # Filter positive length and weight
  mutate(weight_kg = IndWgt/1000) %>% 
  mutate(length_cm = ifelse(LngtCode == ".", 
                            LngtClass/10,
                            LngtClass)) %>% # Standardize length ((https://vocab.ices.dk/?ref=18))
  mutate(keep = ifelse(LngtCode == "." & Year == 2008, "N", "Y")) %>%
  filter(keep == "Y") %>% 
  filter(length_cm < 70)

# Plot
ggplot(bits_ca_fle, aes(IndWgt, length_cm, color = LngtCode)) +
  geom_point() + 
  facet_wrap(~Year)


# Now extract the coefficients for each year (not bothering with outliers at the moment)
fle_intercept <- bits_ca_fle %>%
  split(.$Year) %>%
  purrr::map(~lm(log(IndWgt) ~ log(length_cm), data = .x)) %>%
  purrr::map_df(broom::tidy, .id = 'Year') %>%
  filter(term == "(Intercept)") %>% 
  mutate(a = exp(estimate)) %>% 
  mutate(Year = as.integer(Year)) %>% 
  dplyr::select(Year, a, std.error)

fle_slope <- bits_ca_fle %>%
  split(.$Year) %>%
  purrr::map(~lm(log(IndWgt) ~ log(length_cm), data = .x)) %>%
  purrr::map_df(broom::tidy, .id = 'Year') %>%
  filter(term == "log(length_cm)") %>% 
  mutate(Year = as.integer(Year)) %>% 
  rename("b" = "estimate") %>% 
  dplyr::select(Year, b, std.error)

# Make a plot for the supporting information
#plot(cod_intercept$a ~ cod_slope$b)

pars <- bind_rows(cod_intercept %>% rename(est = a) %>% mutate(par = "a", Species = "Cod"),
                  cod_slope %>% rename(est = b) %>% mutate(par = "b", Species = "Cod"),
                  fle_intercept %>% rename(est = a) %>% mutate(par = "a", Species = "Flounder"),
                  fle_slope %>% rename(est = b) %>% mutate(par = "b", Species = "Flounder")) %>% 
  mutate(lwr = est - 1.96*std.error,
         upr = est + 1.96*std.error) %>% 
  mutate(par = ifelse(par == "a", "Weight-length constant", "Weight-length exponent"))

coefs <- pars %>% 
  filter(Year > 1992) %>% 
  ggplot(aes(x = Year, y = est, color = Species)) + 
  geom_point() + 
  #geom_errorbar(width = 0) + 
  facet_wrap(~par, scales = "free") +
  scale_color_brewer(palette = "Set2") +
  coord_flip() +
  labs(y = "Estimate") + 
  scale_x_continuous(breaks = c(1993:2020)) +
  theme_plot() +
  theme(axis.text.x = element_text(angle = 0))

dd_test <- data.frame(L = rep(seq(1, 30, 1), 2),
                      Year = as.factor(rep(c(1993, 2020), each = 30)),
                      a = rep(c(0.006, 0.015), each = 30),
                      b = rep(c(3.2, 2.9), each = 30)) %>% 
  mutate(W = a*L^b) %>% 
  ggplot(aes(L, W, color = Year)) + 
  scale_color_brewer(palette = "Set1") +
  geom_line() +
  theme_plot() + 
  theme(axis.text.x = element_text(angle = 0),
        aspect.ratio = 3/4)


dd_test2 <- data.frame(L = rep(seq(5, 34, 1), 2),
                       Year = as.factor(rep(c(1993, 2020), each = 30)),
                       a = rep(c(0.006, 0.015), each = 30),
                       b = rep(c(3.2, 2.9), each = 30)) %>% 
  mutate(W = a*L^b) %>% 
  dplyr::select(-a, -b) %>% 
  pivot_wider(names_from = Year, values_from = W) %>% 
  mutate(ratio = `2020` / `1993`) %>% 
  ggplot(aes(L, ratio)) + 
  geom_line() +
  theme_plot() + 
  geom_hline(yintercept = 1, linetype = 2, size = 0.5, color = "grey") +
  labs(y = "Weight ratio 2020:1993") + 
  theme(axis.text.x = element_text(angle = 0),
        aspect.ratio = 3/4)

coefs + (dd_test + dd_test2) + plot_annotation(tag_levels = "A") + plot_layout(heights = c(2, 1))


ggsave("figures/supp/density/annual_LW_pars.png", width = 4.5, height = 6.5, dpi = 600)

cod_intercept <- cod_intercept %>% dplyr::select(-std.error)
cod_slope <- cod_slope %>% dplyr::select(-std.error)
fle_intercept <- fle_intercept %>% dplyr::select(-std.error)
fle_slope <- fle_slope %>% dplyr::select(-std.error)

Join the annual L-W relationships to the respective catch data to calculate CPUE in biomass not abundance

# These are the haul-data
# hlcodL
# hlfleL

hlcodL <- left_join(hlcodL, cod_intercept, by = "Year")
hlcodL <- left_join(hlcodL, cod_slope, by = "Year")

hlfleL <- left_join(hlfleL, fle_intercept, by = "Year")
hlfleL <- left_join(hlfleL, fle_slope, by = "Year")

Convert from CPUE in numbers to kg

# First standardize length to cm and then check how zero-catches are implemented at this stage
hlcodL <- hlcodL %>% 
  mutate(length_cm = ifelse(LngtCode == ".", 
                            LngtClass/10,
                            LngtClass)) # Standardize length ((https://vocab.ices.dk/?ref=18))

filter(hlcodL, length_cm == 0) # No such thing
#> # A tibble: 0 × 50
#> # … with 50 variables: RecordType <chr>, Survey <chr>, Quarter <int>,
#> #   Country <chr>, Ship <chr>, Gear <chr>, SweepLngt <int>, GearEx <chr>,
#> #   DoorType <lgl>, StNo <chr>, HaulNo <int>, Year <int>, SpecCodeType <chr>,
#> #   SpecCode <int>, SpecVal <fct>, Sex <chr>, TotalNo <dbl>,
#> #   CatIdentifier <int>, NoMeas <int>, SubFactor <dbl>, SubWgt <int>,
#> #   CatCatchWgt <int>, LngtCode <chr>, LngtClass <int>, HLNoAtLngt <dbl>,
#> #   DevStage <chr>, LenMeasType <int>, DateofCalculation <int>, …

# Now check if all rows where length is NA are the ones with zero catch!
hlcodL %>% 
  mutate(length2 = replace_na(length_cm, -9),
         no_length = ifelse(length2 < 0, "T", "F")) %>% 
  ggplot(., aes(length2, CPUEun, color = no_length)) + geom_point(alpha = 0.2) + facet_wrap(~no_length)
#> Warning: Removed 5 rows containing missing values (geom_point).


# Right, so all hauls with zero catch have NA length_cm. I don't have any NA catches
t <- hlcodL %>% drop_na(CPUEun)
t <- hlcodL %>% filter(CPUEun == 0)
t <- hlcodL %>% drop_na(length_cm)

# In other words, a zero catch is when the catch is zero and length_cm is NA
# In order to not get any NA CPUEs in unit biomass because length is NA (I want them instead
# to be 0, as the numbers-CPUE is), I will replace length_cm == NA with length_cm == 0 before
# calculating biomass cpue
hlcodL <- hlcodL %>% mutate(length_cm2 = replace_na(length_cm, 0))

# Standardize length in the haul-data and calculate weight
hlcodL <- hlcodL %>% 
  mutate(weight_kg = (a*length_cm2^b)/1000) %>% 
  mutate(CPUEun_kg = weight_kg*CPUEun)

# Plot and check it's correct also in this data
ggplot(hlcodL, aes(weight_kg, length_cm2)) +
  geom_point() + 
  facet_wrap(~Year)


# Hmm, some unrealistic weights actually
hlcodL %>% arrange(desc(weight_kg)) %>% as.data.frame() %>% head(100)
#>     RecordType Survey Quarter Country Ship Gear SweepLngt GearEx DoorType
#> 1           HL   BITS       1     DEN 26D4  GRT        NA      S       NA
#> 2           HL   BITS       1     DEN 26D4  GRT        NA      S       NA
#> 3           HL   BITS       1     DEN 26D4  GRT        NA      S       NA
#> 4           HL   BITS       1     DEN 26D4  GRT        NA      S       NA
#> 5           HL   BITS       1     DEN 26D4  GRT        NA      S       NA
#> 6           HL   BITS       1     DEN 26D4  GRT        NA      S       NA
#> 7           HL   BITS       1     DEN 26D4  GRT        NA      S       NA
#> 8           HL   BITS       1     DEN 26D4  GRT        NA      S       NA
#> 9           HL   BITS       1     DEN 26D4  GRT        NA      S       NA
#> 10          HL   BITS       1     DEN 26D4  GRT        NA      S       NA
#> 11          HL   BITS       1     DEN 26D4  GRT        NA      S       NA
#> 12          HL   BITS       1     DEN 26D4  GRT        NA   <NA>       NA
#> 13          HL   BITS       1     SWE 77AR  FOT       185   <NA>       NA
#> 14          HL   BITS       1     DEN 26D4  GRT        NA      S       NA
#> 15          HL   BITS       1     DEN 26D4  GRT        60   <NA>       NA
#> 16          HL   BITS       1     DEN 26D4  GRT        NA   <NA>       NA
#> 17          HL   BITS       1     SWE 77AR  FOT       225   <NA>       NA
#> 18          HL   BITS       1     GFR 06S1  H20        NA   <NA>       NA
#> 19          HL   BITS       1     DEN 26D4  TVL        75      S       NA
#> 20          HL   BITS       1     LAT 90MX  LBT        NA   <NA>       NA
#> 21          HL   BITS       1     DEN 26D4  GRT        60   <NA>       NA
#> 22          HL   BITS       1     DEN 26D4  GRT        NA   <NA>       NA
#> 23          HL   BITS       4     SWE 77AR  GOV        75   <NA>       NA
#> 24          HL   BITS       4     DEN 26D4  TVL        75      S       NA
#> 25          HL   BITS       1     DEN 26D4  GRT        NA      S       NA
#> 26          HL   BITS       1     GFR 06S1  CHP        NA   <NA>       NA
#> 27          HL   BITS       4     SWE 77AR  TVL        75   <NA>       NA
#> 28          HL   BITS       1     SWE 77AR  GOV       100   <NA>       NA
#> 29          HL   BITS       1     SWE 77AR  GOV       100   <NA>       NA
#> 30          HL   BITS       1     DEN 26D4  GRT        NA   <NA>       NA
#> 31          HL   BITS       1     DEN 26D4  GRT        NA   <NA>       NA
#> 32          HL   BITS       4     SWE 77AR  FOT       225   <NA>       NA
#> 33          HL   BITS       4     SWE 77AR  FOT       203   <NA>       NA
#> 34          HL   BITS       4     SWE 77AR  GOV       100   <NA>       NA
#> 35          HL   BITS       1     SWE 77AR  FOT       225   <NA>       NA
#> 36          HL   BITS       1     DEN 26D4  GRT       110   <NA>       NA
#> 37          HL   BITS       1     DEN 26D4  GRT        NA   <NA>       NA
#> 38          HL   BITS       1     RUS RUJB  TVL        NA   <NA>       NA
#> 39          HL   BITS       1     DEN 26D4  GRT        NA   <NA>       NA
#> 40          HL   BITS       1     GFR 06SL  TVS        NA   <NA>       NA
#> 41          HL   BITS       1     GFR 06S1  CHP        NA   <NA>       NA
#> 42          HL   BITS       1     GFR 06S1  CHP        NA   <NA>       NA
#> 43          HL   BITS       1     DEN 26D4  GRT        60   <NA>       NA
#> 44          HL   BITS       1     SWE 77AR  FOT       225   <NA>       NA
#> 45          HL   BITS       1     GFR 06S1  H20        NA   <NA>       NA
#> 46          HL   BITS       1     DEN 26D4  TVL        NA      S     TRUE
#> 47          HL   BITS       1     SWE 77AR  FOT       185   <NA>       NA
#> 48          HL   BITS       1     DEN 26D4  TVL        75      S       NA
#> 49          HL   BITS       1     LAT 90MX  LBT        NA   <NA>       NA
#> 50          HL   BITS       1     DEN 26D4  GRT        60   <NA>       NA
#> 51          HL   BITS       1     LAT LAIZ  LBT        NA   <NA>       NA
#> 52          HL   BITS       4     SWE 77AR  FOT       180   <NA>       NA
#> 53          HL   BITS       4     POL 67BC  TVL        75      S       NA
#> 54          HL   BITS       1     RUS RUJB  HAK        NA   <NA>       NA
#> 55          HL   BITS       1     RUS RUJB  HAK        NA   <NA>       NA
#> 56          HL   BITS       1     DEN 26D4  TVL        NA      S     TRUE
#> 57          HL   BITS       1     DEN 26D4  GRT        NA   <NA>       NA
#> 58          HL   BITS       4     DEN 26D4  TVL        75      S       NA
#> 59          HL   BITS       1     RUS RUEK   DT        NA   <NA>       NA
#> 60          HL   BITS       1     DEN 26D4  GRT        NA   <NA>       NA
#> 61          HL   BITS       1     DEN 26D4  TVL        NA      S     TRUE
#> 62          HL   BITS       1     DEN 26D4  GRT        NA      S       NA
#> 63          HL   BITS       1     DEN 26D4  GRT        NA   <NA>       NA
#> 64          HL   BITS       1     SWE 77AR  FOT       203   <NA>       NA
#> 65          HL   BITS       1     POL 67BC  TVL        NA   <NA>       NA
#> 66          HL   BITS       1     SWE 77AR  GOV        50   <NA>       NA
#> 67          HL   BITS       1     SWE 77AR  FOT       225   <NA>       NA
#> 68          HL   BITS       1     DEN 26D4  GRT        NA   <NA>       NA
#> 69          HL   BITS       1     RUS RUNT  TVL        NA   <NA>       NA
#> 70          HL   BITS       1     SWE 77AR  TVL        95      S     TRUE
#> 71          HL   BITS       1     GFR 06S1  CHP        NA   <NA>       NA
#> 72          HL   BITS       4     GFR 06S1  H20        NA   <NA>       NA
#> 73          HL   BITS       4     SWE 77AR  FOT       185   <NA>       NA
#> 74          HL   BITS       4     SWE 77AR  GOV        75   <NA>       NA
#> 75          HL   BITS       1     POL 67BC  TVL        NA   <NA>       NA
#> 76          HL   BITS       1     RUS RUJB  HAK        NA   <NA>       NA
#> 77          HL   BITS       1     DEN 26D4  GRT        NA   <NA>       NA
#> 78          HL   BITS       1     GFR 06S1  H20        NA   <NA>       NA
#> 79          HL   BITS       1     DEN 26D4  GRT        NA   <NA>       NA
#> 80          HL   BITS       1     SWE 77AR  FOT       203   <NA>       NA
#> 81          HL   BITS       1     POL 67BC  TVL        75      S     TRUE
#> 82          HL   BITS       1     DEN 26D4  GRT        NA      S       NA
#> 83          HL   BITS       4     SWE 77AR  FOT       225   <NA>       NA
#> 84          HL   BITS       1     LAT 90MX  LBT        NA   <NA>       NA
#> 85          HL   BITS       1     RUS RUNT  TVL        NA   <NA>       NA
#> 86          HL   BITS       1     DEN 26D4  GRT        NA      S       NA
#> 87          HL   BITS       1     RUS RUEK   DT        NA   <NA>       NA
#> 88          HL   BITS       1     DEN 26D4  GRT        60   <NA>       NA
#> 89          HL   BITS       1     POL 67BC  P20        NA   <NA>       NA
#> 90          HL   BITS       1     DEN 26D4  GRT        NA   <NA>       NA
#> 91          HL   BITS       1     RUS RUNT  HAK        NA   <NA>       NA
#> 92          HL   BITS       1     GFR 06S1  H20        NA   <NA>       NA
#> 93          HL   BITS       1     GFR 06S1  TVS        NA   <NA>       NA
#> 94          HL   BITS       4     DEN 26D4  TVL        NA      S     TRUE
#> 95          HL   BITS       4     DEN 26D4  TVL        NA      S     TRUE
#> 96          HL   BITS       4     RUS RUJB  TVL        NA   <NA>       NA
#> 97          HL   BITS       1     DEN 26D4  GRT       110   <NA>       NA
#> 98          HL   BITS       1     RUS RUJB  TVL        NA   <NA>       NA
#> 99          HL   BITS       4     DEN 26D4  TVL        NA      S     TRUE
#> 100         HL   BITS       1     GFR 06S1  H20        NA   <NA>       NA
#>       StNo HaulNo Year SpecCodeType SpecCode SpecVal  Sex TotalNo CatIdentifier
#> 1       83     40 1998            T   164712       1 <NA>      17             1
#> 2       83     40 1998            T   164712       1 <NA>      17             1
#> 3       83     40 1998            T   164712       1 <NA>      17             1
#> 4       83     40 1998            T   164712       1 <NA>      17             1
#> 5       83     40 1998            T   164712       1 <NA>      17             1
#> 6       83     40 1998            T   164712       1 <NA>      17             1
#> 7       83     40 1998            T   164712       1 <NA>      17             1
#> 8       83     40 1998            T   164712       1 <NA>      17             1
#> 9       83     40 1998            T   164712       1 <NA>      17             1
#> 10      83     40 1998            T   164712       1 <NA>      17             1
#> 11      83     40 1998            T   164712       1 <NA>      17             1
#> 12      45     25 1996            W   126436       1 <NA>      93             1
#> 13      80     32 1993            W   126436       1 <NA>     454             1
#> 14      83     40 1998            T   164712       1 <NA>      17             1
#> 15      43     22 1994            T   164712       1 <NA>      37             1
#> 16       9      5 1996            W   126436       1 <NA>     406             1
#> 17     191      3 1998            T   164712       1 <NA>     546             1
#> 18      46     20 1993            W   126436       1 <NA>     326             1
#> 19      71     36 2003            T   164712       1 <NA>     255             1
#> 20  000001     19 1991            W   126436       1 <NA>     282             1
#> 21     117     50 1994            T   164712       1 <NA>     775             1
#> 22      75     40 1996            W   126436       1 <NA>      29             1
#> 23     584     18 2000            W   126436       1 <NA>     454             1
#> 24      10      4 2002            T   164712       1 <NA>    1958             1
#> 25     136     64 1998            T   164712       1 <NA>     130             1
#> 26   26073     50 1991            W   126436       1 <NA>      77             1
#> 27     573     11 2002            W   126436       1 <NA>    3090             1
#> 28     220     21 2000            W   126436       1 <NA>    1076             1
#> 29     237     34 2000            W   126436       1 <NA>     638             1
#> 30     106     57 1991            W   126436       1 <NA>      51             1
#> 31     111     47 1996            W   126436       1 <NA>      78             1
#> 32     231     26 1996            W   126436       1 <NA>      54             1
#> 33     247      2 1995            W   126436       1 <NA>    2736             1
#> 34     652     17 1999            T   164712       1 <NA>    1484             1
#> 35     207     15 1998            T   164712       1 <NA>      20             1
#> 36     162     72 1993            W   126436       1 <NA>     128             1
#> 37      53     30 1996            W   126436       1 <NA>     197             1
#> 38    <NA>     13 2006            W   126436       1 <NA>      24             1
#> 39      57     23 1995            W   126436       1 <NA>     223             1
#> 40   24316     41 2008            T   164712       1 <NA>     864             1
#> 41   26021     29 1992            W   126436       1 <NA>      68             1
#> 42   26073     50 1991            W   126436       1 <NA>      77             1
#> 43      74     39 1994            T   164712       1 <NA>     457             1
#> 44     222     26 1998            T   164712       1 <NA>     126             1
#> 45      44     24 1994            T   164712       1 <NA>     260             1
#> 46      95     66 2005            W   126436       1 <NA>     764             1
#> 47      58     10 1993            W   126436       1 <NA>      85             1
#> 48       6      3 2001            T   164712       1 <NA>     132             1
#> 49  000001     12 1991            W   126436       1 <NA>      49             1
#> 50      17      9 1993            W   126436       1 <NA>     281             1
#> 51  000001      6 1993            W   126436       1 <NA>      22             1
#> 52     252     21 1993            W   126436       1 <NA>    5932             1
#> 53   26132     24 2008            T   164712       1 <NA>    1696             1
#> 54    <NA>     35 1998            T   164712       1 <NA>    1214             1
#> 55    <NA>     33 1998            T   164712       1 <NA>     302             1
#> 56      19     17 2009            W   126436       1 <NA>     972             1
#> 57      81     44 1991            W   126436       1 <NA>     164             1
#> 58      94     46 2000            T   164712       1 <NA>     180             1
#> 59    <NA>     13 1996            W   126436       1 <NA>     194             1
#> 60      33     14 1995            W   126436       1 <NA>      32             1
#> 61     100      1 2010            W   126436       1 <NA>     552             1
#> 62      16      8 2000            W   126436       1 <NA>      85             1
#> 63      66     36 1996            W   126436       1 <NA>      43             1
#> 64      74     23 1996            W   126436       1 <NA>     936             1
#> 65       1      1 2004            W   126436       1 <NA>     470             1
#> 66      85     37 1993            W   126436       1 <NA>    1332             1
#> 67     199      8 1998            T   164712       1 <NA>    1016             1
#> 68     123     46 1995            W   126436       1 <NA>     231             1
#> 69      60     17 2010            T   164712       1 <NA>     566             1
#> 70     267     52 2009            W   126436       1    U     650             1
#> 71   26021     29 1992            W   126436       1 <NA>      68             1
#> 72      25     72 1994            T   164712       1 <NA>     428             1
#> 73     287     32 1994            W   126436       1 <NA>    2924             1
#> 74     594     27 2000            W   126436       1 <NA>     842             1
#> 75       2      2 2004            W   126436       1 <NA>     286             1
#> 76    <NA>     37 1998            T   164712       1 <NA>     668             1
#> 77      11      3 1995            W   126436       1 <NA>      80             1
#> 78      73     34 1994            T   164712       1 <NA>     764             1
#> 79      51     29 1996            W   126436       1 <NA>     212             1
#> 80      95     44 1996            W   126436       1 <NA>     248             1
#> 81   26211      3 2011            W   126436       1    U    1130             1
#> 82      18      7 1997            T   164712       1 <NA>      75             1
#> 83     756     26 1997            W   126436       1 <NA>      22             1
#> 84  000001     11 1991            W   126436       1 <NA>      82             1
#> 85      60     23 2010            T   164712       1 <NA>    6475             1
#> 86     130     61 1998            T   164712       1 <NA>     493             1
#> 87    <NA>     19 1995            W   126436       1 <NA>     466             1
#> 88      63     32 1994            T   164712       1 <NA>      40             1
#> 89     Mar     18 1994            T   164712       1 <NA>      48             1
#> 90      46     26 1996            W   126436       1 <NA>     110             1
#> 91    <NA>      5 1999            T   164712       1 <NA>      94             1
#> 92      48     22 1993            W   126436       1 <NA>     586             1
#> 93     902     34 2001            T   164712       1 <NA>     278             1
#> 94     179     16 2009            W   126436       1 <NA>     943             1
#> 95       7     32 2006            W   126436       1 <NA>     234             2
#> 96    <NA>     60 2003            T   164712       1 <NA>     194             1
#> 97      79     55 1992            W   126436       1 <NA>       4             1
#> 98      56     40 2011            T   164712       1 <NA>    4360             1
#> 99      39     42 2011            W   126436       1 <NA>     710             1
#> 100     47     51 1996            W   126436       1 <NA>     640             1
#>     NoMeas SubFactor SubWgt CatCatchWgt LngtCode LngtClass HLNoAtLngt DevStage
#> 1       17         1     NA       13275        0       335          1     <NA>
#> 2       17         1     NA       13275        0       285          1     <NA>
#> 3       17         1     NA       13275        0       225          2     <NA>
#> 4       17         1     NA       13275        0       220          1     <NA>
#> 5       17         1     NA       13275        0       215          1     <NA>
#> 6       17         1     NA       13275        0       180          1     <NA>
#> 7       17         1     NA       13275        0       175          1     <NA>
#> 8       17         1     NA       13275        0       160          1     <NA>
#> 9       17         1     NA       13275        0       150          2     <NA>
#> 10      17         1     NA       13275        0       145          3     <NA>
#> 11      17         1     NA       13275        0       140          1     <NA>
#> 12      93         1 113900      113900        .      1360          1     <NA>
#> 13     454         1     NA      234500        1       127          1     <NA>
#> 14      17         1     NA       13275        0       130          2     <NA>
#> 15      37         1     NA        1033        1       127          1     <NA>
#> 16     406         1 257280      257280        .      1230          1     <NA>
#> 17     273         1     NA      484399        1       121          2     <NA>
#> 18     163         1     NA        2190        1       116          2     <NA>
#> 19     255         1     NA      158200        1       121          1     <NA>
#> 20     141         1     NA        2390        1       112          2     <NA>
#> 21     775         1     NA        5718        1       118          1     <NA>
#> 22      29         1  48300       48300        .      1170          1     <NA>
#> 23     227         1     NA      268580        1       118          2     <NA>
#> 24    1958         1     NA     1040200        1       118          1     <NA>
#> 25     130         1     NA      157570        1       116          1     <NA>
#> 26      77         1     NA        1540        1       110          1     <NA>
#> 27    1545         1     NA     1302400        1       117          2     <NA>
#> 28     538         1     NA      303000        1       116          2     <NA>
#> 29     319         1     NA      158200        1       115          2     <NA>
#> 30      51         1     NA         599        1       108          1     <NA>
#> 31      78         1 119000      119000        .      1130          1     <NA>
#> 32      27         1     NA       96200        1       113          2     <NA>
#> 33    1368         1     NA      607800        1       111          2     <NA>
#> 34     742         1     NA      267459        1       113          2     <NA>
#> 35      10         1     NA       76000        1       113          2     <NA>
#> 36     128         1     NA        1079        1       109          1     <NA>
#> 37     197         1 192900      192900        .      1120          1     <NA>
#> 38      12         1     NA       39544        1       118          2     <NA>
#> 39     223         1 149100      149100        .      1100          1     <NA>
#> 40     864         1     NA      247830        1       119          1     <NA>
#> 41      68         1     NA        2535        1       109          1     <NA>
#> 42      77         1     NA        1540        1       106          1     <NA>
#> 43     457         1     NA          42        1       112          1     <NA>
#> 44      63         1     NA       48400        1       111          2     <NA>
#> 45     130         1     NA        2374        1       111          2     <NA>
#> 46     764         1 139400      139400        .      1150          1     <NA>
#> 47      85         1     NA       46000        1       107          1     <NA>
#> 48     132         1     NA       61000        1       111          1     <NA>
#> 49      49         1     NA        1013        1       104          1     <NA>
#> 50     277         1     NA        1297        1       106          1     <NA>
#> 51      22         1     NA         219        1       106          1     <NA>
#> 52    1499         1     NA     5688600        1       106          3     <NA>
#> 53     848         1     NA      968860        1       116          2     <NA>
#> 54     390         1     NA       16278        1       109          3     <NA>
#> 55     151         1     NA        2962        1       109          2     <NA>
#> 56     972         1 322476      322476        .      1130          1     <NA>
#> 57     164         1     NA        3531        1       103          1     <NA>
#> 58     180         1     NA       46605        1       109          1     <NA>
#> 59      97         1     NA        3360        1       108          2     <NA>
#> 60      32         1  47100       47100        .      1060          1     <NA>
#> 61     552         1 419300      419300        .      1130          1     <NA>
#> 62      85         1     NA       37470        1       108          1     <NA>
#> 63      43         1  67365       67365        .      1070          1     <NA>
#> 64     468         1     NA      597000        1       107          2     <NA>
#> 65     235         1     NA       98500        1       110          2     <NA>
#> 66    1332         1     NA      574000        1       104          1     <NA>
#> 67     508         1     NA      268000        1       107          2     <NA>
#> 68     231         1 204000      204000        .      1050          1     <NA>
#> 69     283         1     NA      369740        1       112          2     <NA>
#> 70     325         1     NA      253940        1       111          2     <NA>
#> 71      68         1     NA        2535        1       104          1     <NA>
#> 72     214         1     NA      326000        1       107          2     <NA>
#> 73    1462         1     NA     1872800        1       107          2     <NA>
#> 74     421         1     NA      382800        1       107          2     <NA>
#> 75     143         1     NA      185600        1       109          2     <NA>
#> 76     291         1     NA        9063        1       106          2     <NA>
#> 77      80         1  66500       66500        .      1040          1     <NA>
#> 78     382         1     NA       10120        1       106          2     <NA>
#> 79     212         1 324500      324500        .      1050          1     <NA>
#> 80     124         1     NA      419000        1       105          2     <NA>
#> 81     565         1 216000      432000        1       110          2     <NA>
#> 82      75         1     NA      108824        1       105          1     <NA>
#> 83      11         1     NA       74600        1       105          2     <NA>
#> 84      41         1     NA        1500        1       100          2     <NA>
#> 85     413         1     NA     5844000        1       110         16     <NA>
#> 86     493         1     NA      577785        1       105          1     <NA>
#> 87     233         1     NA        4381        1       103          2     <NA>
#> 88      40         1     NA         761        1       105          1     <NA>
#> 89      24         1     NA       28400        1       105          2     <NA>
#> 90     110         1 108900      108900        .      1040          1     <NA>
#> 91      47         1     NA        1155        1       104          2     <NA>
#> 92     293         1     NA        2710        1       101          2     <NA>
#> 93     139         1     NA        1266        1       105          2     <NA>
#> 94     943         1 430200      430200        .      1080          1     <NA>
#> 95     234         1 121600      121600        .      1090          1     <NA>
#> 96      97         1     NA      137200        1       105          2     <NA>
#> 97       4         1     NA         151        1       101          1     <NA>
#> 98     639         1     NA     1289466        1       108          2     <NA>
#> 99     710         1 323900      323900        .      1080          1     <NA>
#> 100    165         1     NA        5280        1       103          4     <NA>
#>     LenMeasType DateofCalculation Valid_Aphia Ship2 Ship3
#> 1            NA          20140617      126436  DAN2  DAN2
#> 2            NA          20140617      126436  DAN2  DAN2
#> 3            NA          20140617      126436  DAN2  DAN2
#> 4            NA          20140617      126436  DAN2  DAN2
#> 5            NA          20140617      126436  DAN2  DAN2
#> 6            NA          20140617      126436  DAN2  DAN2
#> 7            NA          20140617      126436  DAN2  DAN2
#> 8            NA          20140617      126436  DAN2  DAN2
#> 9            NA          20140617      126436  DAN2  DAN2
#> 10           NA          20140617      126436  DAN2  DAN2
#> 11           NA          20140617      126436  DAN2  DAN2
#> 12           NA          20190208      126436  DAN2  DAN2
#> 13           NA          20211203      126436   ARG   ARG
#> 14           NA          20140617      126436  DAN2  DAN2
#> 15           NA          20161213      126436  DAN2  DAN2
#> 16           NA          20190208      126436  DAN2  DAN2
#> 17           NA          20140617      126436   ARG   ARG
#> 18           NA          20211203      126436   SOL   SOL
#> 19           NA          20131108      126436  DAN2  DAN2
#> 20           NA          20211203      126436  90MX  90MX
#> 21           NA          20161213      126436  DAN2  DAN2
#> 22           NA          20190208      126436  DAN2  DAN2
#> 23           NA          20131112      126436   ARG   ARG
#> 24           NA          20131113      126436  DAN2  DAN2
#> 25           NA          20140617      126436  DAN2  DAN2
#> 26           NA          20211203      126436   SOL   SOL
#> 27           NA          20131113      126436   ARG   ARG
#> 28           NA          20190228      126436   ARG   ARG
#> 29           NA          20190228      126436   ARG   ARG
#> 30           NA          20211203      126436  DAN2  DAN2
#> 31           NA          20190208      126436  DAN2  DAN2
#> 32           NA          20161115      126436   ARG   ARG
#> 33           NA          20161115      126436   ARG   ARG
#> 34           NA          20131112      126436   ARG   ARG
#> 35           NA          20140617      126436   ARG   ARG
#> 36           NA          20211203      126436  DAN2  DAN2
#> 37           NA          20190208      126436  DAN2  DAN2
#> 38           NA          20200115      126436  ATLD  ATLD
#> 39           NA          20190207      126436  DAN2  DAN2
#> 40           NA          20180423      126436  SOL2  SOL2
#> 41           NA          20211203      126436   SOL   SOL
#> 42           NA          20211203      126436   SOL   SOL
#> 43           NA          20161213      126436  DAN2  DAN2
#> 44           NA          20140617      126436   ARG   ARG
#> 45           NA          20161213      126436   SOL   SOL
#> 46           NA          20131204      126436  DAN2  DAN2
#> 47           NA          20211203      126436   ARG   ARG
#> 48           NA          20160714      126436  DAN2  DAN2
#> 49           NA          20211203      126436  90MX  90MX
#> 50           NA          20211203      126436  DAN2  DAN2
#> 51           NA          20211203      126436  LAIZ  LAIZ
#> 52           NA          20131111      126436   ARG   ARG
#> 53           NA          20180507      126436   BAL   BAL
#> 54           NA          20140617      126436  ATLD  ATLD
#> 55           NA          20140617      126436  ATLD  ATLD
#> 56           NA          20131216      126436  DAN2  DAN2
#> 57           NA          20211203      126436  DAN2  DAN2
#> 58           NA          20131112      126436  DAN2  DAN2
#> 59           NA          20190208      126436  RUEK  RUEK
#> 60           NA          20190207      126436  DAN2  DAN2
#> 61           NA          20180423      126436  DAN2  DAN2
#> 62           NA          20190228      126436  DAN2  DAN2
#> 63           NA          20190208      126436  DAN2  DAN2
#> 64           NA          20190208      126436   ARG   ARG
#> 65           NA          20160721      126436   BAL   BAL
#> 66           NA          20211203      126436   ARG   ARG
#> 67           NA          20140617      126436   ARG   ARG
#> 68           NA          20190207      126436  DAN2  DAN2
#> 69           NA          20180423      126436   ATL   ATL
#> 70           NA          20131216      126436   ARG   ARG
#> 71           NA          20211203      126436   SOL   SOL
#> 72           NA          20161115      126436   SOL   SOL
#> 73           NA          20161115      126436   ARG   ARG
#> 74           NA          20131112      126436   ARG   ARG
#> 75           NA          20160721      126436   BAL   BAL
#> 76           NA          20140617      126436  ATLD  ATLD
#> 77           NA          20190207      126436  DAN2  DAN2
#> 78           NA          20161213      126436   SOL   SOL
#> 79           NA          20190208      126436  DAN2  DAN2
#> 80           NA          20190208      126436   ARG   ARG
#> 81           NA          20140611      126436   BAL   BAL
#> 82           NA          20161115      126436  DAN2  DAN2
#> 83           NA          20161115      126436   ARG   ARG
#> 84           NA          20211203      126436  90MX  90MX
#> 85           NA          20180423      126436   ATL   ATL
#> 86           NA          20140617      126436  DAN2  DAN2
#> 87           NA          20190207      126436  RUEK  RUEK
#> 88           NA          20161213      126436  DAN2  DAN2
#> 89           NA          20161213      126436   BAL   BAL
#> 90           NA          20190208      126436  DAN2  DAN2
#> 91           NA          20140616      126436   ATL   ATL
#> 92           NA          20211203      126436   SOL   SOL
#> 93           NA          20160714      126436   SOL   SOL
#> 94           NA          20160721      126436  DAN2  DAN2
#> 95           NA          20131216      126436  DAN2  DAN2
#> 96           NA          20131113      126436  ATLD  ATLD
#> 97           NA          20211203      126436  DAN2  DAN2
#> 98           NA          20140611      126436  ATLD  ATLD
#> 99           NA          20140617      126436  DAN2  DAN2
#> 100          NA          20190208      126436   SOL   SOL
#>                               IDx sub_div Rect HaulVal StdSpecRecCode
#> 1       1998.1.DEN.26D4.GRT.83.40      25 38G5       V              1
#> 2       1998.1.DEN.26D4.GRT.83.40      25 38G5       V              1
#> 3       1998.1.DEN.26D4.GRT.83.40      25 38G5       V              1
#> 4       1998.1.DEN.26D4.GRT.83.40      25 38G5       V              1
#> 5       1998.1.DEN.26D4.GRT.83.40      25 38G5       V              1
#> 6       1998.1.DEN.26D4.GRT.83.40      25 38G5       V              1
#> 7       1998.1.DEN.26D4.GRT.83.40      25 38G5       V              1
#> 8       1998.1.DEN.26D4.GRT.83.40      25 38G5       V              1
#> 9       1998.1.DEN.26D4.GRT.83.40      25 38G5       V              1
#> 10      1998.1.DEN.26D4.GRT.83.40      25 38G5       V              1
#> 11      1998.1.DEN.26D4.GRT.83.40      25 38G5       V              1
#> 12      1996.1.DEN.26D4.GRT.45.25      26 41H0       V              1
#> 13      1993.1.SWE.77AR.FOT.80.32      25 39G5       V              1
#> 14      1998.1.DEN.26D4.GRT.83.40      25 38G5       V              1
#> 15      1994.1.DEN.26D4.GRT.43.22      26 41H0       V              1
#> 16        1996.1.DEN.26D4.GRT.9.5      25 38G5       V              1
#> 17      1998.1.SWE.77AR.FOT.191.3      25 40G5       V              1
#> 18      1993.1.GFR.06S1.H20.46.20      24 39G4       V              1
#> 19      2003.1.DEN.26D4.TVL.71.36      25 39G5       V              1
#> 20  1991.1.LAT.90MX.LBT.000001.19      26 40H0       V              1
#> 21     1994.1.DEN.26D4.GRT.117.50      25 38G5       V              1
#> 22      1996.1.DEN.26D4.GRT.75.40      25 39G6       V              1
#> 23     2000.4.SWE.77AR.GOV.584.18      25 40G5       V              1
#> 24       2002.4.DEN.26D4.TVL.10.4      24 39G4       V              1
#> 25     1998.1.DEN.26D4.GRT.136.64      26 39G8       V              1
#> 26   1991.1.GFR.06S1.CHP.26073.50      26 39G8       V              1
#> 27     2002.4.SWE.77AR.TVL.573.11      25 41G7       V              1
#> 28     2000.1.SWE.77AR.GOV.220.21      24 39G3       V              1
#> 29     2000.1.SWE.77AR.GOV.237.34      25 40G5       V              1
#> 30     1991.1.DEN.26D4.GRT.106.57      25 39G7       V              1
#> 31     1996.1.DEN.26D4.GRT.111.47      25 39G5       V              1
#> 32     1996.4.SWE.77AR.FOT.231.26      28 43G8       V              1
#> 33      1995.4.SWE.77AR.FOT.247.2      25 40G5       V              1
#> 34     1999.4.SWE.77AR.GOV.652.17      25 40G5       V              1
#> 35     1998.1.SWE.77AR.FOT.207.15      25 40G7       V              1
#> 36     1993.1.DEN.26D4.GRT.162.72      25 38G5       V              1
#> 37      1996.1.DEN.26D4.GRT.53.30      26 41G9       V              1
#> 38      2006.1.RUS.RUJB.TVL.NA.13      26 39H0       V              1
#> 39      1995.1.DEN.26D4.GRT.57.23      26 41G9       V              1
#> 40   2008.1.GFR.06SL.TVS.24316.41      24 39G4       V              1
#> 41   1992.1.GFR.06S1.CHP.26021.29      26 41G8       V              1
#> 42   1991.1.GFR.06S1.CHP.26073.50      26 39G8       V              1
#> 43      1994.1.DEN.26D4.GRT.74.39      26 41G8       V              1
#> 44     1998.1.SWE.77AR.FOT.222.26      28 43H0       V              1
#> 45      1994.1.GFR.06S1.H20.44.24      24 39G3       V              1
#> 46      2005.1.DEN.26D4.TVL.95.66      25 39G5       V              1
#> 47      1993.1.SWE.77AR.FOT.58.10      25 41G7       V              1
#> 48        2001.1.DEN.26D4.TVL.6.3      24 39G3       V              1
#> 49  1991.1.LAT.90MX.LBT.000001.12      26 39H0       V              1
#> 50       1993.1.DEN.26D4.GRT.17.9      24 39G4       V              1
#> 51   1993.1.LAT.LAIZ.LBT.000001.6      26 40H0       V              1
#> 52     1993.4.SWE.77AR.FOT.252.21      25 41G7       V              1
#> 53   2008.4.POL.67BC.TVL.26132.24      26 38G8       V              3
#> 54      1998.1.RUS.RUJB.HAK.NA.35      26 40G9       V              1
#> 55      1998.1.RUS.RUJB.HAK.NA.33      26 40G9       V              1
#> 56      2009.1.DEN.26D4.TVL.19.17      24 39G4       V              1
#> 57      1991.1.DEN.26D4.GRT.81.44      26 41G9       V              1
#> 58      2000.4.DEN.26D4.TVL.94.46      25 39G6       V              1
#> 59       1996.1.RUS.RUEK.DT.NA.13      26 39G8       V              1
#> 60      1995.1.DEN.26D4.GRT.33.14      26 41G8       V              1
#> 61      2010.1.DEN.26D4.TVL.100.1      25 39G6       V              1
#> 62       2000.1.DEN.26D4.GRT.16.8      25 40G5       V              1
#> 63      1996.1.DEN.26D4.GRT.66.36      25 39G7       V              1
#> 64      1996.1.SWE.77AR.FOT.74.23      25 40G6       V              1
#> 65        2004.1.POL.67BC.TVL.1.1      26 38G8       V              3
#> 66      1993.1.SWE.77AR.GOV.85.37      24 38G3       V              1
#> 67      1998.1.SWE.77AR.FOT.199.8      24 39G3       V              1
#> 68     1995.1.DEN.26D4.GRT.123.46      25 40G6       V              1
#> 69      2010.1.RUS.RUNT.TVL.60.17      26 39G8       V              1
#> 70     2009.1.SWE.77AR.TVL.267.52      25 40G4       V              1
#> 71   1992.1.GFR.06S1.CHP.26021.29      26 41G8       V              1
#> 72      1994.4.GFR.06S1.H20.25.72      24 38G3       V              1
#> 73     1994.4.SWE.77AR.FOT.287.32      25 40G6       V              1
#> 74     2000.4.SWE.77AR.GOV.594.27      25 40G7       V              1
#> 75        2004.1.POL.67BC.TVL.2.2      26 39G8       V              3
#> 76      1998.1.RUS.RUJB.HAK.NA.37      26 40H0       V              1
#> 77       1995.1.DEN.26D4.GRT.11.3      25 39G7       V              1
#> 78      1994.1.GFR.06S1.H20.73.34      25 40G4       V              1
#> 79      1996.1.DEN.26D4.GRT.51.29      26 41G9       V              1
#> 80      1996.1.SWE.77AR.FOT.95.44      27 43G7       V              1
#> 81    2011.1.POL.67BC.TVL.26211.3      26 38G9       V              1
#> 82       1997.1.DEN.26D4.GRT.18.7      25 40G6       V              1
#> 83     1997.4.SWE.77AR.FOT.756.26      26 41G8       V              1
#> 84  1991.1.LAT.90MX.LBT.000001.11      26 41H0       V              1
#> 85      2010.1.RUS.RUNT.TVL.60.23      26 38G9       V              1
#> 86     1998.1.DEN.26D4.GRT.130.61      26 40G9       V              1
#> 87       1995.1.RUS.RUEK.DT.NA.19      26 38G9       V              1
#> 88      1994.1.DEN.26D4.GRT.63.32      26 39G8       V              1
#> 89     1994.1.POL.67BC.P20.Mar.18      26 38G8       V              1
#> 90      1996.1.DEN.26D4.GRT.46.26      26 41G9       V              1
#> 91       1999.1.RUS.RUNT.HAK.NA.5      26 38G9       V              1
#> 92      1993.1.GFR.06S1.H20.48.22      24 39G4       V              1
#> 93     2001.1.GFR.06S1.TVS.902.34      24 39G4       V              3
#> 94     2009.4.DEN.26D4.TVL.179.16      25 38G6       V              1
#> 95       2006.4.DEN.26D4.TVL.7.32      25 38G5       V              1
#> 96      2003.4.RUS.RUJB.TVL.NA.60      26 39H0       V              1
#> 97      1992.1.DEN.26D4.GRT.79.55      26 39G8       V              1
#> 98      2011.1.RUS.RUJB.TVL.56.40      26 39G9       V              1
#> 99      2011.4.DEN.26D4.TVL.39.42      25 40G4       V              1
#> 100     1996.1.GFR.06S1.H20.47.51      24 39G4       V              1
#>     BySpecRecCode Fishing.line DataType HaulDur GroundSpeed
#> 1               1        -9.00        R      60         3.0
#> 2               1        -9.00        R      60         3.0
#> 3               1        -9.00        R      60         3.0
#> 4               1        -9.00        R      60         3.0
#> 5               1        -9.00        R      60         3.0
#> 6               1        -9.00        R      60         3.0
#> 7               1        -9.00        R      60         3.0
#> 8               1        -9.00        R      60         3.0
#> 9               1        -9.00        R      60         3.0
#> 10              1        -9.00        R      60         3.0
#> 11              1        -9.00        R      60         3.0
#> 12              1        -9.00        R      60         3.4
#> 13              1        83.00        C      60         3.0
#> 14              1        -9.00        R      60         3.0
#> 15              1        -9.00        C      60        -9.0
#> 16              1        -9.00        R      60         3.6
#> 17              1        83.00        C      30         3.3
#> 18              1        36.00        C      30         4.0
#> 19              1        63.46        R      30         3.0
#> 20              1        28.00        C      30        -9.0
#> 21              1        -9.00        C      60        -9.0
#> 22              1        -9.00        R      60         3.2
#> 23              1       160.00        C      30         3.7
#> 24              1        63.46        R      30         3.1
#> 25              1        -9.00        R      60         3.3
#> 26              1        35.20        C      60         3.5
#> 27              1        63.46        C      30         3.0
#> 28              1       160.00        C      30         3.5
#> 29              1       160.00        C      30         3.5
#> 30              1        -9.00        C      60        -9.0
#> 31              1        -9.00        R      60         3.5
#> 32              1        83.00        C      30         3.3
#> 33              1        83.00        C      30         3.7
#> 34              1       160.00        C      30         3.3
#> 35              1        83.00        C      30         3.3
#> 36              1        -9.00        C      60        -9.0
#> 37              1        -9.00        R      60         3.1
#> 38              1        63.46        C      30         3.0
#> 39              1        -9.00        R      60        -9.0
#> 40              1        33.22        R      30         3.2
#> 41              1        35.20        C      60         3.4
#> 42              1        35.20        C      60         3.5
#> 43              1        -9.00        C      60        -9.0
#> 44              1        83.00        C      30         3.4
#> 45              1        36.00        C      30         3.8
#> 46              1        63.46        R      30         3.0
#> 47              1        83.00        C      60         3.5
#> 48              1        63.46        R      30         3.0
#> 49              1        28.00        C      60        -9.0
#> 50              1        -9.00        C      59        -9.0
#> 51              1        28.00        C      60        -9.0
#> 52              1        83.00        C      30         3.0
#> 53              0        63.46        C      30         3.0
#> 54              1        -9.00        C      30         3.6
#> 55              1        -9.00        C      30         3.6
#> 56              1        63.46        R      30         2.9
#> 57              1        -9.00        C      60        -9.0
#> 58              1        63.46        R      31         2.9
#> 59              1        -9.00        C      30         3.0
#> 60              1        -9.00        R      60        -9.0
#> 61              1        63.46        R      30         3.0
#> 62              1        -9.00        R      60         2.9
#> 63              1        -9.00        R      60         3.1
#> 64              1        83.00        C      30         3.4
#> 65              0        63.46        C      30        -9.0
#> 66              1       160.00        C      60         2.9
#> 67              1        83.00        C      30         3.5
#> 68              1        -9.00        R      60        -9.0
#> 69              1        63.46        C      30         2.9
#> 70              1        63.46        C      30         3.0
#> 71              1        35.20        C      60         3.4
#> 72              1        36.00        C      30         3.4
#> 73              1        83.00        C      30         3.2
#> 74              1       160.00        C      30         3.5
#> 75              0        63.46        C      30        -9.0
#> 76              1        -9.00        C      60         3.9
#> 77              1        -9.00        R      60        -9.0
#> 78              1        36.00        C      30         3.2
#> 79              1        -9.00        R      60         3.0
#> 80              1        83.00        C      30         3.8
#> 81              1        63.46        C      30         3.0
#> 82              1        -9.00        R      60         3.3
#> 83              1        83.00        C      30         3.3
#> 84              1        28.00        C      30        -9.0
#> 85              1        63.46        C      15         3.0
#> 86              1        -9.00        R      60         3.2
#> 87              1        -9.00        C      30         3.3
#> 88              1        -9.00        C      60        -9.0
#> 89              0        39.80        C      30        -9.0
#> 90              1        -9.00        R      60         3.3
#> 91              1        -9.00        C      30         3.5
#> 92              1        36.00        C      30         3.6
#> 93              1        33.22        C      30         3.2
#> 94              1        63.46        R      30         2.8
#> 95              1        63.46        R      31         3.3
#> 96              1        63.46        C      30         3.0
#> 97              1        -9.00        C      60        -9.0
#> 98              1        63.46        C      30         3.0
#> 99              1        63.46        R      29         1.7
#> 100             1        36.00        C      30         4.0
#>                           haul.id ShootLat ShootLong Depth      Species
#> 1       1998:1:DEN:DAN2:GRT:83:40  54.8150   15.3217    71 Gadus morhua
#> 2       1998:1:DEN:DAN2:GRT:83:40  54.8150   15.3217    71 Gadus morhua
#> 3       1998:1:DEN:DAN2:GRT:83:40  54.8150   15.3217    71 Gadus morhua
#> 4       1998:1:DEN:DAN2:GRT:83:40  54.8150   15.3217    71 Gadus morhua
#> 5       1998:1:DEN:DAN2:GRT:83:40  54.8150   15.3217    71 Gadus morhua
#> 6       1998:1:DEN:DAN2:GRT:83:40  54.8150   15.3217    71 Gadus morhua
#> 7       1998:1:DEN:DAN2:GRT:83:40  54.8150   15.3217    71 Gadus morhua
#> 8       1998:1:DEN:DAN2:GRT:83:40  54.8150   15.3217    71 Gadus morhua
#> 9       1998:1:DEN:DAN2:GRT:83:40  54.8150   15.3217    71 Gadus morhua
#> 10      1998:1:DEN:DAN2:GRT:83:40  54.8150   15.3217    71 Gadus morhua
#> 11      1998:1:DEN:DAN2:GRT:83:40  54.8150   15.3217    71 Gadus morhua
#> 12      1996:1:DEN:DAN2:GRT:45:25  56.3700   20.0666    70 Gadus morhua
#> 13       1993:1:SWE:ARG:FOT:80:32  55.4120   15.3205    92 Gadus morhua
#> 14      1998:1:DEN:DAN2:GRT:83:40  54.8150   15.3217    71 Gadus morhua
#> 15      1994:1:DEN:DAN2:GRT:43:22  56.4500   20.2000    81 Gadus morhua
#> 16        1996:1:DEN:DAN2:GRT:9:5  54.5750   15.3200    60 Gadus morhua
#> 17       1998:1:SWE:ARG:FOT:191:3  55.8218   15.4220    53 Gadus morhua
#> 18       1993:1:GFR:SOL:H20:46:20  55.1333   14.1667    48 Gadus morhua
#> 19      2003:1:DEN:DAN2:TVL:71:36  55.3709   15.5140    97 Gadus morhua
#> 20  1991:1:LAT:90MX:LBT:000001:19  55.6167   20.0833    80 Gadus morhua
#> 21     1994:1:DEN:DAN2:GRT:117:50  54.7833   15.9500    57 Gadus morhua
#> 22      1996:1:DEN:DAN2:GRT:75:40  55.1983   16.4916    56 Gadus morhua
#> 23      2000:4:SWE:ARG:GOV:584:18  55.8150   15.3917    52 Gadus morhua
#> 24       2002:4:DEN:DAN2:TVL:10:4  55.4908   14.6121    58 Gadus morhua
#> 25     1998:1:DEN:DAN2:GRT:136:64  55.4800   18.4683    90 Gadus morhua
#> 26    1991:1:GFR:SOL:CHP:26073:50  55.2500   18.3500    81 Gadus morhua
#> 27      2002:4:SWE:ARG:TVL:573:11  56.1460   17.7615    63 Gadus morhua
#> 28      2000:1:SWE:ARG:GOV:220:21  55.0100   13.9300    48 Gadus morhua
#> 29      2000:1:SWE:ARG:GOV:237:34  55.8467   15.5717    41 Gadus morhua
#> 30     1991:1:DEN:DAN2:GRT:106:57  55.3167   17.3167    80 Gadus morhua
#> 31     1996:1:DEN:DAN2:GRT:111:47  55.2150   15.6083    98 Gadus morhua
#> 32      1996:4:SWE:ARG:FOT:231:26  57.0833   18.9000    90 Gadus morhua
#> 33       1995:4:SWE:ARG:FOT:247:2  55.8323   15.5427    38 Gadus morhua
#> 34      1999:4:SWE:ARG:GOV:652:17  55.8400   15.5600    44 Gadus morhua
#> 35      1998:1:SWE:ARG:FOT:207:15  55.9271   17.1108    47 Gadus morhua
#> 36     1993:1:DEN:DAN2:GRT:162:72  54.8500   15.6667    79 Gadus morhua
#> 37      1996:1:DEN:DAN2:GRT:53:30  56.2566   19.5666    82 Gadus morhua
#> 38      2006:1:RUS:ATLD:TVL:NA:13  55.1583   20.0267    60 Gadus morhua
#> 39      1995:1:DEN:DAN2:GRT:57:23  56.3666   19.8666    89 Gadus morhua
#> 40   2008:1:GFR:SOL2:TVS:24316:41  55.1505   14.0718    46 Gadus morhua
#> 41    1992:1:GFR:SOL:CHP:26021:29  56.1500   18.5333   110 Gadus morhua
#> 42    1991:1:GFR:SOL:CHP:26073:50  55.2500   18.3500    81 Gadus morhua
#> 43      1994:1:DEN:DAN2:GRT:74:39  56.1167   18.2667    75 Gadus morhua
#> 44      1998:1:SWE:ARG:FOT:222:26  57.2520   20.7436    72 Gadus morhua
#> 45       1994:1:GFR:SOL:H20:44:24  55.0333   13.4333    48 Gadus morhua
#> 46      2005:1:DEN:DAN2:TVL:95:66  55.0966   15.2680    67 Gadus morhua
#> 47       1993:1:SWE:ARG:FOT:58:10  56.0352   17.7188    64 Gadus morhua
#> 48        2001:1:DEN:DAN2:TVL:6:3  55.0214   13.7642    47 Gadus morhua
#> 49  1991:1:LAT:90MX:LBT:000001:12  55.1167   20.3333    40 Gadus morhua
#> 50       1993:1:DEN:DAN2:GRT:17:9  55.0333   14.2167    48 Gadus morhua
#> 51   1993:1:LAT:LAIZ:LBT:000001:6  55.5667   20.6500    60 Gadus morhua
#> 52      1993:4:SWE:ARG:FOT:252:21  56.1133   17.5917    42 Gadus morhua
#> 53    2008:4:POL:BAL:TVL:26132:24  54.5400   18.8867    65 Gadus morhua
#> 54      1998:1:RUS:ATLD:HAK:NA:35  55.5167   19.8833    85 Gadus morhua
#> 55      1998:1:RUS:ATLD:HAK:NA:33  55.6167   19.6500    80 Gadus morhua
#> 56      2009:1:DEN:DAN2:TVL:19:17  55.3195   14.9859    72 Gadus morhua
#> 57      1991:1:DEN:DAN2:GRT:81:44  56.2333   19.5167   108 Gadus morhua
#> 58      2000:4:DEN:DAN2:TVL:94:46  55.1058   16.3850    63 Gadus morhua
#> 59       1996:1:RUS:RUEK:DT:NA:13  55.0833   18.4333    76 Gadus morhua
#> 60      1995:1:DEN:DAN2:GRT:33:14  56.4166   18.7166    94 Gadus morhua
#> 61      2010:1:DEN:DAN2:TVL:100:1  55.0897   16.3814    62 Gadus morhua
#> 62       2000:1:DEN:DAN2:GRT:16:8  55.7672   15.7198    54 Gadus morhua
#> 63      1996:1:DEN:DAN2:GRT:66:36  55.4566   17.8650    70 Gadus morhua
#> 64       1996:1:SWE:ARG:FOT:74:23  55.6700   16.4667    65 Gadus morhua
#> 65         2004:1:POL:BAL:TVL:1:1  54.5667   18.8167    61 Gadus morhua
#> 66       1993:1:SWE:ARG:GOV:85:37  54.9983   13.9913    49 Gadus morhua
#> 67       1998:1:SWE:ARG:FOT:199:8  55.0090   13.9270    48 Gadus morhua
#> 68     1995:1:DEN:DAN2:GRT:123:46  55.8166   16.6666    46 Gadus morhua
#> 69       2010:1:RUS:ATL:TVL:60:17  55.3617   18.0833    81 Gadus morhua
#> 70      2009:1:SWE:ARG:TVL:267:52  55.6688   14.5024    51 Gadus morhua
#> 71    1992:1:GFR:SOL:CHP:26021:29  56.1500   18.5333   110 Gadus morhua
#> 72       1994:4:GFR:SOL:H20:25:72  54.7167   13.1333    26 Gadus morhua
#> 73      1994:4:SWE:ARG:FOT:287:32  55.8717   16.0733    55 Gadus morhua
#> 74      2000:4:SWE:ARG:GOV:594:27  55.9362   17.0593    47 Gadus morhua
#> 75         2004:1:POL:BAL:TVL:2:2  55.2333   18.1667    61 Gadus morhua
#> 76      1998:1:RUS:ATLD:HAK:NA:37  55.5167   20.0333    82 Gadus morhua
#> 77       1995:1:DEN:DAN2:GRT:11:3  55.3166   17.2166    75 Gadus morhua
#> 78       1994:1:GFR:SOL:H20:73:34  55.6167   14.8667    76 Gadus morhua
#> 79      1996:1:DEN:DAN2:GRT:51:29  56.3450   19.6316   110 Gadus morhua
#> 80       1996:1:SWE:ARG:FOT:95:44  57.4800   17.5533    73 Gadus morhua
#> 81     2011:1:POL:BAL:TVL:26211:3  54.5267   19.3583    75 Gadus morhua
#> 82       1997:1:DEN:DAN2:GRT:18:7  55.6783   16.7883    47 Gadus morhua
#> 83      1997:4:SWE:ARG:FOT:756:26  56.2205   18.4318    72 Gadus morhua
#> 84  1991:1:LAT:90MX:LBT:000001:11  56.1167   20.5167    30 Gadus morhua
#> 85       2010:1:RUS:ATL:TVL:60:23  54.9217   19.5692    85 Gadus morhua
#> 86     1998:1:DEN:DAN2:GRT:130:61  55.6167   19.4983    87 Gadus morhua
#> 87       1995:1:RUS:RUEK:DT:NA:19  54.9833   19.6500    71 Gadus morhua
#> 88      1994:1:DEN:DAN2:GRT:63:32  55.4667   18.5333    90 Gadus morhua
#> 89      1994:1:POL:BAL:P20:Mar:18  54.9000   18.6167    50 Gadus morhua
#> 90      1996:1:DEN:DAN2:GRT:46:26  56.4433   19.9866    80 Gadus morhua
#> 91        1999:1:RUS:ATL:HAK:NA:5  54.9500   19.6333    72 Gadus morhua
#> 92       1993:1:GFR:SOL:H20:48:22  55.0667   14.2833    48 Gadus morhua
#> 93      2001:1:GFR:SOL:TVS:902:34  55.0833   14.1500    50 Gadus morhua
#> 94     2009:4:DEN:DAN2:TVL:179:16  54.9337   16.2348    45 Gadus morhua
#> 95       2006:4:DEN:DAN2:TVL:7:32  54.9951   15.3587    57 Gadus morhua
#> 96      2003:4:RUS:ATLD:TVL:NA:60  55.2083   20.1500    51 Gadus morhua
#> 97      1992:1:DEN:DAN2:GRT:79:55  55.4667   18.2167    74 Gadus morhua
#> 98      2011:1:RUS:ATLD:TVL:56:40  55.3583   19.9267    82 Gadus morhua
#> 99      2011:4:DEN:DAN2:TVL:39:42  55.7076   14.7245    53 Gadus morhua
#> 100      1996:1:GFR:SOL:H20:47:51  55.1333   14.2667    49 Gadus morhua
#>        CPUEun           a        b length_cm length_cm2 weight_kg CPUEun_kg
#> 1    1.000000 0.007345618 3.082202       335        335 445.37563 445.37563
#> 2    1.000000 0.007345618 3.082202       285        285 270.61760 270.61760
#> 3    2.000000 0.007345618 3.082202       225        225 130.59602 261.19205
#> 4    1.000000 0.007345618 3.082202       220        220 121.85635 121.85635
#> 5    1.000000 0.007345618 3.082202       215        215 113.52063 113.52063
#> 6    1.000000 0.007345618 3.082202       180        180  65.64985  65.64985
#> 7    1.000000 0.007345618 3.082202       175        175  60.19004  60.19004
#> 8    1.000000 0.007345618 3.082202       160        160  45.66372  45.66372
#> 9    2.000000 0.007345618 3.082202       150        150  37.42667  74.85333
#> 10   3.000000 0.007345618 3.082202       145        145  33.71329 101.13986
#> 11   1.000000 0.007345618 3.082202       140        140  30.25718  30.25718
#> 12   1.000000 0.008367352 3.055881       136        136  27.69677  27.69677
#> 13   1.000000 0.006985661 3.112176       127        127  24.63852  24.63852
#> 14   2.000000 0.007345618 3.082202       130        130  24.07846  48.15692
#> 15   1.000000 0.008339286 3.052078       127        127  21.98379  21.98379
#> 16   1.000000 0.008367352 3.055881       123        123  20.37462  20.37462
#> 17   2.000000 0.007345618 3.082202       121        121  19.30161  38.60322
#> 18   2.000000 0.006985661 3.112176       116        116  18.58505  37.17010
#> 19   2.000000 0.007444717 3.070310       121        121  18.47755  36.95509
#> 20   2.000000 0.006436894 3.144784       112        112  17.90706  35.81411
#> 21   1.000000 0.008339286 3.052078       118        118  17.56609  17.56609
#> 22   1.000000 0.008367352 3.055881       117        117  17.48711  17.48711
#> 23   2.000000 0.007554303 3.071838       118        118  17.48561  34.97121
#> 24   2.000000 0.007243730 3.076256       118        118  17.12383  34.24765
#> 25   1.000000 0.007345618 3.082202       116        116  16.94747  16.94747
#> 26   1.000000 0.006436894 3.144784       110        110  16.92058  16.92058
#> 27   2.000000 0.007243730 3.076256       117        117  16.68132  33.36265
#> 28   2.000000 0.007554303 3.071838       116        116  16.59110  33.18221
#> 29   2.000000 0.007554303 3.071838       115        115  16.15566  32.31132
#> 30   1.000000 0.006436894 3.144784       108        108  15.97183  15.97183
#> 31   1.000000 0.008367352 3.055881       113        113  15.72359  15.72359
#> 32   2.000000 0.008367352 3.055881       113        113  15.72359  31.44717
#> 33   2.000000 0.007303857 3.095567       111        111  15.66706  31.33412
#> 34   2.000000 0.006830676 3.097913       113        113  15.65748  31.31495
#> 35   2.000000 0.007345618 3.082202       113        113  15.63258  31.26517
#> 36   1.000000 0.006985661 3.112176       109        109  15.31218  15.31218
#> 37   1.000000 0.008367352 3.055881       112        112  15.30223  15.30223
#> 38   2.000000 0.009208543 3.001584       118        118  15.24467  30.48935
#> 39   1.000000 0.007303857 3.095567       110        110  15.23425  15.23425
#> 40   2.000000 0.009197813 2.994628       119        119  15.10690  30.21379
#> 41   1.000000 0.007017543 3.107987       109        109  15.08273  15.08273
#> 42   1.000000 0.006436894 3.144784       106        106  15.06002  15.06002
#> 43   1.000000 0.008339286 3.052078       112        112  14.97969  14.97969
#> 44   2.000000 0.007345618 3.082202       111        111  14.79541  29.59081
#> 45   2.000000 0.008339286 3.052078       111        111  14.57521  29.15042
#> 46   2.000000 0.008377248 3.027632       115        115  14.52564  29.05128
#> 47   1.000000 0.006985661 3.112176       107        107  14.45462  14.45462
#> 48   2.000000 0.007693063 3.064660       111        111  14.26658  28.53316
#> 49   1.000000 0.006436894 3.144784       104        104  14.18438  14.18438
#> 50   1.000000 0.006985661 3.112176       106        106  14.03833  14.03833
#> 51   1.000000 0.006985661 3.112176       106        106  14.03833  14.03833
#> 52   3.000000 0.006985661 3.112176       106        106  14.03833  42.11499
#> 53   2.000000 0.009197813 2.994628       116        116  13.99484  27.98968
#> 54   3.000000 0.007345618 3.082202       109        109  13.98905  41.96716
#> 55   2.000000 0.007345618 3.082202       109        109  13.98905  27.97811
#> 56   2.000000 0.007746254 3.044723       113        113  13.80850  27.61701
#> 57   1.000000 0.006436894 3.144784       103        103  13.75987  13.75987
#> 58   1.935484 0.007554303 3.071838       109        109  13.70373  26.52336
#> 59   2.000000 0.008367352 3.055881       108        108  13.69270  27.38541
#> 60   1.000000 0.007303857 3.095567       106        106  13.58386  13.58386
#> 61   2.000000 0.007920229 3.035684       113        113  13.52806  27.05613
#> 62   1.000000 0.007554303 3.071838       108        108  13.32119  13.32119
#> 63   1.000000 0.008367352 3.055881       107        107  13.30894  13.30894
#> 64   2.000000 0.008367352 3.055881       107        107  13.30894  26.61788
#> 65   2.000000 0.007991791 3.046753       110        110  13.25143  26.50287
#> 66   1.000000 0.006985661 3.112176       104        104  13.23031  13.23031
#> 67   2.000000 0.007345618 3.082202       107        107  13.21293  26.42585
#> 68   1.000000 0.007303857 3.095567       105        105  13.19107  13.19107
#> 69   2.000000 0.007920229 3.035684       112        112  13.16790  26.33580
#> 70   2.000000 0.007746254 3.044723       111        111  13.07776  26.15552
#> 71   1.000000 0.007017543 3.107987       104        104  13.03462  13.03462
#> 72   2.000000 0.008339286 3.052078       107        107  13.03068  26.06136
#> 73   2.000000 0.008339286 3.052078       107        107  13.03068  26.06136
#> 74   2.000000 0.007554303 3.071838       107        107  12.94592  25.89184
#> 75   2.000000 0.007991791 3.046753       109        109  12.88780  25.77560
#> 76   2.000000 0.007345618 3.082202       106        106  12.83601  25.67202
#> 77   1.000000 0.007303857 3.095567       104        104  12.80604  12.80604
#> 78   2.000000 0.008339286 3.052078       106        106  12.66255  25.32509
#> 79   1.000000 0.008367352 3.055881       105        105  12.56325  12.56325
#> 80   2.000000 0.008367352 3.055881       105        105  12.56325  25.12651
#> 81   2.000000 0.007917341 3.037179       110        110  12.55028  25.10056
#> 82   1.000000 0.008046923 3.063870       105        105  12.53982  12.53982
#> 83   2.000000 0.008046923 3.063870       105        105  12.53982  25.07964
#> 84   2.000000 0.006436894 3.144784       100        100  12.53846  25.07692
#> 85  16.000000 0.007920229 3.035684       110        110  12.46698 199.47172
#> 86   1.000000 0.007345618 3.082202       105        105  12.46643  12.46643
#> 87   2.000000 0.007303857 3.095567       103        103  12.42870  24.85740
#> 88   1.000000 0.008339286 3.052078       105        105  12.30147  12.30147
#> 89   2.000000 0.008339286 3.052078       105        105  12.30147  24.60294
#> 90   1.000000 0.008367352 3.055881       104        104  12.20119  12.20119
#> 91   2.000000 0.006830676 3.097913       104        104  12.10758  24.21515
#> 92   2.000000 0.006985661 3.112176       101        101  12.07836  24.15672
#> 93   2.000000 0.007693063 3.064660       105        105  12.03257  24.06513
#> 94   2.000000 0.007746254 3.044723       108        108  12.03105  24.06210
#> 95   1.935484 0.009208543 3.001584       109        109  12.01426  23.25341
#> 96   2.000000 0.007444717 3.070310       105        105  11.95433  23.90866
#> 97   1.000000 0.007017543 3.107987       101        101  11.90117  11.90117
#> 98   2.000000 0.007917341 3.037179       108        108  11.86999  23.73998
#> 99   2.068966 0.007917341 3.037179       108        108  11.86999  24.55860
#> 100  4.000000 0.008367352 3.055881       103        103  11.84620  47.38482
hlcodL <- hlcodL %>% filter(weight_kg < 100)

ggplot(hlcodL, aes(weight_kg, length_cm2)) +
  geom_point() + 
  facet_wrap(~Year)


# Now do the same for flounder
# First standardize length to cm and then check how zero-catches are implemented at this stage
hlfleL <- hlfleL %>% 
  mutate(length_cm = ifelse(LngtCode %in% c(".", "0"), 
                            LngtClass/10,
                            LngtClass)) # Standardize length (https://vocab.ices.dk/?ref=18)

filter(hlfleL, length_cm == 0) # No such thing
#> # A tibble: 0 × 50
#> # … with 50 variables: RecordType <chr>, Survey <chr>, Quarter <int>,
#> #   Country <chr>, Ship <chr>, Gear <chr>, SweepLngt <int>, GearEx <chr>,
#> #   DoorType <lgl>, StNo <chr>, HaulNo <int>, Year <int>, SpecCodeType <chr>,
#> #   SpecCode <int>, SpecVal <fct>, Sex <chr>, TotalNo <dbl>,
#> #   CatIdentifier <int>, NoMeas <int>, SubFactor <dbl>, SubWgt <int>,
#> #   CatCatchWgt <int>, LngtCode <chr>, LngtClass <int>, HLNoAtLngt <dbl>,
#> #   DevStage <chr>, LenMeasType <int>, DateofCalculation <int>, …

bits_ca_fle <- bits_ca_fle %>% 
  drop_na(IndWgt) %>% 
  drop_na(LngtClass) %>% 
  filter(IndWgt > 0 & LngtClass > 0) %>%  # Filter positive length and weight
  mutate(weight_kg = IndWgt/1000) %>% 
  mutate(length_cm = ifelse(LngtCode == ".", 
                            LngtClass/10,
                            LngtClass)) %>% # Standardize length ((https://vocab.ices.dk/?ref=18))
  mutate(keep = ifelse(LngtCode == "." & Year == 2008, "N", "Y")) %>%
  filter(keep == "Y") %>% 
  filter(length_cm < 70)

# Now check if all rows where length is NA are the ones with zero catch!
hlfleL %>% 
  mutate(length2 = replace_na(length_cm, -9),
         no_length = ifelse(length2 < 0, "T", "F")) %>% 
  ggplot(., aes(length2, CPUEun, color = no_length)) + geom_point(alpha = 0.2) + facet_wrap(~no_length)
#> Warning: Removed 42 rows containing missing values (geom_point).


hlfleL %>% mutate(length2 = replace_na(length_cm, -9)) %>% group_by(length2) %>% distinct(CPUEun) %>% arrange(CPUEun)
#> # A tibble: 12,467 × 2
#> # Groups:   length2 [253]
#>    CPUEun length2
#>     <dbl>   <dbl>
#>  1  0        -9  
#>  2  0.667    19  
#>  3  0.667    21  
#>  4  0.667    35  
#>  5  0.667    39  
#>  6  0.667    40  
#>  7  0.667    42  
#>  8  0.870    27  
#>  9  0.870    32  
#> 10  0.870    37.5
#> # … with 12,457 more rows

# Right, so all hauls with zero catch have NA length_cm. I don't have any NA catches
t <- hlfleL %>% drop_na(CPUEun)
# Well, 11 rows. I will remove them
hlfleL <- hlfleL %>% drop_na(CPUEun)
t <- hlfleL %>% filter(CPUEun == 0)
t <- hlfleL %>% drop_na(length_cm)

# In other words, a zero catch is when the catch is zero and length_cm is NA
# In order to not get any NA CPUEs in unit biomass because length is NA (I want them instead
# to be 0, as the numbers-CPUE is), I will replace length_cm == NA with length_cm == 0 before
# calculating biomass cpue
hlfleL <- hlfleL %>% mutate(length_cm2 = replace_na(length_cm, 0))

# Standardize length in the haul-data and calculate weight
hlfleL <- hlfleL %>% 
  mutate(weight_kg = (a*length_cm2^b)/1000) %>% 
  mutate(CPUEun_kg = weight_kg*CPUEun)

# Plot and check it's correct also in this data
ggplot(hlfleL, aes(weight_kg, length_cm2)) +
  geom_point() + 
  facet_wrap(~Year)
#> Warning: Removed 3311 rows containing missing values (geom_point).


# Check
t <- hlfleL %>% drop_na(CPUEun_kg) # Should not have any NA in biomass-catch
t <- hlfleL %>% filter(CPUEun_kg == 0) # Should result in a few percent of rows (note this is not proportion of hauls, but rows)
t <- hlfleL %>% drop_na(length_cm2) # Should be no NA

# What is the proportion of zero-catch hauls?
cod_0plot <- hlcodL %>%
  group_by(haul.id, Year, Quarter) %>%
  summarise(CPUEun_haul = sum(CPUEun)) %>% 
  ungroup() %>% 
  mutate(zero_catch = ifelse(CPUEun_haul == 0, "Y", "N")) %>% 
  group_by(Year, Quarter, zero_catch) %>% 
  summarise(n = n()) %>% 
  ungroup() %>% 
  pivot_wider(names_from = zero_catch, values_from = n) %>% 
  mutate(prop_zero_catch_hauls = Y/(N+Y)) %>% 
  ggplot(., aes(Year, prop_zero_catch_hauls)) + geom_bar(stat = "identity") + 
  coord_cartesian(expand = 0, ylim = c(0, 1)) + 
  facet_wrap(~ Quarter) +
  ggtitle("Cod")

# How many zero-catch hauls?
fle_0plot <- hlfleL %>%
  group_by(haul.id, Year, Quarter) %>%
  summarise(CPUEun_haul = sum(CPUEun)) %>% 
  ungroup() %>% 
  mutate(zero_catch = ifelse(CPUEun_haul == 0, "Y", "N")) %>% 
  group_by(Year, Quarter, zero_catch) %>% 
  summarise(n = n()) %>% 
  ungroup() %>% 
  pivot_wider(names_from = zero_catch, values_from = n) %>% 
  mutate(prop_zero_catch_hauls = Y/(N+Y)) %>% 
  ggplot(., aes(Year, prop_zero_catch_hauls)) + geom_bar(stat = "identity") + 
  coord_cartesian(expand = 0, ylim = c(0, 1)) + 
  facet_wrap(~ Quarter) +
  ggtitle("Flounder")

cod_0plot / fle_0plot
#> Warning: Removed 1 rows containing missing values (position_stack).
#> Warning: Removed 1 rows containing missing values (position_stack).

Standardize according to Orio

To get unit: kg of fish caught by trawling for 1 h a standard bottom swept area of 0.45km2 using a TVL trawl with 75 m sweeps at the standard speed of three knots

# Remove hauls done with the TVL gear with a SweepLngt < 50 (these are calibration hauls, pers. com. Anders & Ale)
# And also hauls without length-information
# Remove pelagic gear
hlcodL <- hlcodL %>%
  mutate(SweepLngt2 = replace_na(SweepLngt, 50)) %>% 
  mutate(keep = ifelse(Gear == "TVL" & SweepLngt2 < 50, "N", "Y")) %>% 
  filter(keep == "Y") %>% 
  dplyr::select(-keep, -SweepLngt2) %>% 
  filter(!Gear == "PEL")
  
hlfleL <- hlfleL %>%
  mutate(SweepLngt2 = replace_na(SweepLngt, 50)) %>% 
  mutate(keep = ifelse(Gear == "TVL" & SweepLngt2 < 50, "N", "Y")) %>% 
  filter(keep == "Y") %>% 
  dplyr::select(-keep, -SweepLngt2) %>% 
  filter(!Gear == "PEL")

# Add in RS and RSA-values from the sweep file
# CPUE should be multiplied with RS and RSA to standardize to a relative speed and gear dimension.
# There is not a single file will all RS and RSA values. Instead they come in three files:
# - sweep (non-Swedish hauls between 1991-2016)
# - + calculated based on trawl speed and gear dimensions.
# I will join in the RS and RSA values from all sources, then standardize and filter
# away non-standardized hauls
# sort(unique(sweep$Year))
# sort(unique(sweep$Country))

# Since I don't have the sweep data for Swedish data, I have to calculate it from scratch using the 
# equation in Orio's spreadsheet

# First I will join in the sweep data, 
sweep_sel <- sweep %>% rename("haul.id" = "ï..haul.id") %>% dplyr::select(haul.id, RSA, RS)

hlcodL2 <- left_join(hlcodL, sweep_sel)
hlfleL2 <- left_join(hlfleL, sweep_sel)

hlcodL2 <- hlcodL2 %>%
  rename("RS_sweep" = "RS",
         "RSA_sweep" = "RSA") %>% 
  mutate(RS_sweep = as.numeric(RS_sweep),
         RSA_sweep = as.numeric(RSA_sweep))

hlfleL2 <- hlfleL2 %>%
  rename("RS_sweep" = "RS",
         "RSA_sweep" = "RSA") %>% 
  mutate(RS_sweep = as.numeric(RS_sweep),
         RSA_sweep = as.numeric(RSA_sweep))

sort(colnames(hlcodL2))
#>  [1] "a"                 "b"                 "BySpecRecCode"    
#>  [4] "CatCatchWgt"       "CatIdentifier"     "Country"          
#>  [7] "CPUEun"            "CPUEun_kg"         "DataType"         
#> [10] "DateofCalculation" "Depth"             "DevStage"         
#> [13] "DoorType"          "Fishing.line"      "Gear"             
#> [16] "GearEx"            "GroundSpeed"       "haul.id"          
#> [19] "HaulDur"           "HaulNo"            "HaulVal"          
#> [22] "HLNoAtLngt"        "IDx"               "length_cm"        
#> [25] "length_cm2"        "LenMeasType"       "LngtClass"        
#> [28] "LngtCode"          "NoMeas"            "Quarter"          
#> [31] "RecordType"        "Rect"              "RS_sweep"         
#> [34] "RSA_sweep"         "Sex"               "Ship"             
#> [37] "Ship2"             "Ship3"             "ShootLat"         
#> [40] "ShootLong"         "SpecCode"          "SpecCodeType"     
#> [43] "Species"           "SpecVal"           "StdSpecRecCode"   
#> [46] "StNo"              "sub_div"           "SubFactor"        
#> [49] "SubWgt"            "Survey"            "SweepLngt"        
#> [52] "TotalNo"           "Valid_Aphia"       "weight_kg"        
#> [55] "Year"
sort(colnames(hlfleL2))
#>  [1] "a"                 "b"                 "BySpecRecCode"    
#>  [4] "CatCatchWgt"       "CatIdentifier"     "Country"          
#>  [7] "CPUEun"            "CPUEun_kg"         "DataType"         
#> [10] "DateofCalculation" "Depth"             "DevStage"         
#> [13] "DoorType"          "Fishing.line"      "Gear"             
#> [16] "GearEx"            "GroundSpeed"       "haul.id"          
#> [19] "HaulDur"           "HaulNo"            "HaulVal"          
#> [22] "HLNoAtLngt"        "IDx"               "length_cm"        
#> [25] "length_cm2"        "LenMeasType"       "LngtClass"        
#> [28] "LngtCode"          "NoMeas"            "Quarter"          
#> [31] "RecordType"        "Rect"              "RS_sweep"         
#> [34] "RSA_sweep"         "Sex"               "Ship"             
#> [37] "Ship2"             "Ship3"             "ShootLat"         
#> [40] "ShootLong"         "SpecCode"          "SpecCodeType"     
#> [43] "Species"           "SpecVal"           "StdSpecRecCode"   
#> [46] "StNo"              "sub_div"           "SubFactor"        
#> [49] "SubWgt"            "Survey"            "SweepLngt"        
#> [52] "TotalNo"           "Valid_Aphia"       "weight_kg"        
#> [55] "Year"

# I will calculate a RS and RSA column in the catch data based on Ale's equation in the sweep file:
sort(unique(hlcodL2$GroundSpeed))
#>  [1] -9.0  0.1  0.2  0.8  1.7  1.8  2.0  2.1  2.2  2.3  2.4  2.5  2.6  2.7  2.8
#> [16]  2.9  3.0  3.1  3.2  3.3  3.4  3.5  3.6  3.7  3.8  3.9  4.0  4.1  4.2  4.3
#> [31]  4.4  4.5  4.6  4.7  4.9  5.0  5.2  5.3  5.4  5.5  5.6  5.7  5.9  6.0  6.1
#> [46]  6.2  6.3  6.6  6.7  6.8  6.9  7.1  7.3  8.6
sort(unique(hlcodL2$Fishing.line))
#>  [1]  -9.00  16.54  28.00  33.22  35.20  36.00  39.80  63.46  83.00 160.00
sort(unique(hlcodL2$SweepLngt))
#>  [1]   0  40  50  60  75  87  90  95 100 110 125 135 180 182 185 188 200 203 225
#> [20] 235

# First replace -9 in the columns I use for the calculations with NA so I don't end up with real numbers that are wrong!
hlcodL2 <- hlcodL2 %>% mutate(GroundSpeed = ifelse(GroundSpeed == -9, NA, GroundSpeed),
                              Fishing.line = ifelse(Fishing.line == -9, NA, Fishing.line),
                              SweepLngt = ifelse(SweepLngt == -9, NA, SweepLngt))

hlfleL2 <- hlfleL2 %>% mutate(GroundSpeed = ifelse(GroundSpeed == -9, NA, GroundSpeed),
                              Fishing.line = ifelse(Fishing.line == -9, NA, Fishing.line),
                              SweepLngt = ifelse(SweepLngt == -9, NA, SweepLngt))

hlcodL2 %>% filter(Quarter == 1) %>%
  distinct(GroundSpeed, Fishing.line, SweepLngt) %>% as.data.frame()
#>     SweepLngt Fishing.line GroundSpeed
#> 1          NA           NA          NA
#> 2          NA        35.20         3.5
#> 3          NA        28.00          NA
#> 4          NA        35.20         3.6
#> 5          NA        35.20         3.4
#> 6          NA        36.00          NA
#> 7         110           NA          NA
#> 8          NA        39.80          NA
#> 9          60           NA          NA
#> 10         NA        35.20          NA
#> 11         NA        35.20         3.0
#> 12         NA        35.20         3.1
#> 13         NA        35.20         3.2
#> 14         NA        36.00         3.0
#> 15         50       160.00         3.3
#> 16         50       160.00         3.2
#> 17         50       160.00         3.0
#> 18         50       160.00         3.1
#> 19         50       160.00         3.4
#> 20        100       160.00         3.1
#> 21         50       160.00         2.8
#> 22         50       160.00         2.7
#> 23        185        83.00         3.1
#> 24        125        83.00         3.1
#> 25        185        83.00         3.2
#> 26        185        83.00         3.3
#> 27         50       160.00         2.9
#> 28         NA        36.00         3.8
#> 29         NA        36.00         3.6
#> 30         NA        36.00         4.0
#> 31         NA        36.00         4.6
#> 32         NA        36.00         3.4
#> 33         NA        36.00         4.2
#> 34         NA        36.00         3.2
#> 35         NA        36.00         2.6
#> 36         NA        36.00         2.8
#> 37         50       160.00         2.6
#> 38        185        83.00         3.0
#> 39        185        83.00         2.8
#> 40        185        83.00         3.4
#> 41        185        83.00         3.5
#> 42        185        83.00         2.7
#> 43        185        83.00         2.9
#> 44         NA        36.00         4.4
#> 45         NA        36.00         2.4
#> 46        180        83.00         3.1
#> 47        180        83.00         3.2
#> 48        180        83.00         3.0
#> 49        180        83.00         3.3
#> 50        180        83.00         3.4
#> 51        180        83.00         3.5
#> 52        180        83.00         2.8
#> 53        180        83.00         3.9
#> 54         NA        36.00         3.7
#> 55         NA        36.00         3.1
#> 56         NA           NA         3.2
#> 57         NA           NA         3.3
#> 58         NA           NA         3.6
#> 59         NA           NA         3.4
#> 60         90       160.00         3.4
#> 61         NA           NA         3.0
#> 62         NA           NA         3.8
#> 63         NA           NA         3.9
#> 64         NA           NA         3.7
#> 65         90       160.00         3.2
#> 66         90       160.00         3.8
#> 67         90       160.00         3.7
#> 68         90       160.00         3.5
#> 69         90       160.00         3.6
#> 70         90       160.00         3.0
#> 71         90       160.00         3.9
#> 72         NA           NA         4.3
#> 73         NA           NA         3.1
#> 74         NA           NA         4.9
#> 75         NA           NA         4.0
#> 76         NA           NA         4.6
#> 77         NA           NA         2.9
#> 78         NA           NA         4.2
#> 79         NA           NA         3.5
#> 80         NA           NA         4.7
#> 81        203        83.00         3.8
#> 82        203        83.00         3.5
#> 83        203        83.00         3.9
#> 84        203        83.00         3.7
#> 85        203        83.00         4.0
#> 86         NA           NA         2.8
#> 87        203        83.00         3.6
#> 88        203        83.00         3.4
#> 89         NA           NA         2.6
#> 90         NA           NA         2.7
#> 91         NA           NA         2.4
#> 92         NA           NA         4.1
#> 93        235        83.00         3.7
#> 94        235        83.00         4.1
#> 95        235        83.00         3.9
#> 96        235        83.00         3.8
#> 97        235        83.00         3.0
#> 98        235        83.00         3.6
#> 99        235        83.00         4.0
#> 100       235        83.00         3.1
#> 101       235        83.00         3.5
#> 102       135        83.00         3.9
#> 103       135        83.00         3.7
#> 104       135        83.00         3.5
#> 105       135        83.00         3.6
#> 106       135        83.00         3.2
#> 107       225        83.00         3.5
#> 108       225        83.00         3.4
#> 109       225        83.00         2.7
#> 110       225        83.00         3.3
#> 111       225        83.00         3.6
#> 112       225        83.00         3.2
#> 113       225        83.00         3.7
#> 114         0        83.00         3.6
#> 115       225        83.00         3.1
#> 116        NA        63.46          NA
#> 117        NA           NA         4.4
#> 118        NA        36.00         1.7
#> 119       203        83.00         3.3
#> 120        NA        36.00         3.9
#> 121        75        63.46         3.1
#> 122        75        63.46         3.2
#> 123        75        63.46         3.0
#> 124        75        63.46         2.6
#> 125        75        63.46         2.9
#> 126       235        83.00         3.2
#> 127       235        83.00         3.3
#> 128       235        83.00         3.4
#> 129       100       160.00         3.6
#> 130       100       160.00         3.8
#> 131       100       160.00         3.5
#> 132       100       160.00         3.7
#> 133       100       160.00         3.4
#> 134       100       160.00         3.3
#> 135        NA        33.22         3.1
#> 136        NA        33.22         3.3
#> 137        NA        33.22         3.0
#> 138        NA        33.22         3.2
#> 139        NA        33.22         3.4
#> 140        NA        63.46         3.1
#> 141        NA        63.46         3.0
#> 142        NA        63.46         2.8
#> 143        NA        63.46         2.9
#> 144        75        63.46         2.7
#> 145        75        63.46         2.8
#> 146        NA        63.46         3.2
#> 147        75        33.22          NA
#> 148        NA        63.46         3.3
#> 149        NA        63.46         3.4
#> 150        75        63.46         3.3
#> 151        NA        33.22         2.9
#> 152        75        63.46         2.4
#> 153        NA        33.22         2.8
#> 154        60        63.46         3.0
#> 155        60        63.46         2.9
#> 156        60        63.46         3.1
#> 157        60        63.46         2.8
#> 158        NA        63.46         2.5
#> 159        NA        63.46         2.7
#> 160        95        63.46         3.1
#> 161        95        63.46         3.0
#> 162        NA        33.22          NA
#> 163        95        63.46          NA
#> 164        NA        33.22         3.5
#> 165        NA        33.22         3.6
#> 166        75        63.46          NA
#> 167        95        63.46         2.9
#> 168        NA        63.46         3.7
#> 169        NA        63.46         2.2
#> 170        NA        33.22         3.8
#> 171        NA        63.46         2.3
#> 172        NA        63.46         2.6
#> 173        NA        33.22         3.7
#> 174        NA        33.22         2.7
#> 175        NA        63.46         2.0
#> 176        NA        63.46         0.1
#> 177        NA        63.46         2.1
#> 178        NA        63.46         1.7
#> 179        NA        63.46         2.4
#> 180        NA        33.22         2.6
#> 181        NA        63.46         3.9
#> 182        NA        33.22         2.3
#> 183       200        33.22         2.9
#> 184        NA        63.46         8.6
#> 185        75        63.46         3.4
#> 186        NA        63.46         0.2
#> 187        75        63.46         3.6
#> 188        75        63.46         3.5
#> 189        75        63.46         2.5
#> 190        NA        16.54         3.4
#> 191       185        83.00         2.6
#> 192        50       160.00         3.6
#> 193       203        83.00         3.2
#> 194       110       160.00         3.6
#> 195       110       160.00         3.1
#> 196       110       160.00         3.4
#> 197        NA        63.46         3.6
#> 198        NA        63.46         3.8
#> 199        NA        63.46         4.2
#> 200        NA        63.46         5.5
#> 201        NA        63.46         5.2
#> 202        NA        63.46         6.6
#> 203        NA        63.46         5.0
#> 204        NA        63.46         6.1
#> 205        NA        63.46         6.2
#> 206        NA        63.46         6.0
#> 207        NA        63.46         5.9
#> 208        NA        63.46         6.8
#> 209        NA        63.46         4.1
#> 210        NA        63.46         4.3

hlcodL2 %>% filter(Quarter == 4) %>%
  distinct(GroundSpeed, Fishing.line, SweepLngt) %>% as.data.frame()
#>     SweepLngt Fishing.line GroundSpeed
#> 1          NA        36.00          NA
#> 2          NA        36.00         3.4
#> 3          NA        36.00         3.6
#> 4          NA        36.00         3.2
#> 5          NA        36.00         3.8
#> 6          NA        36.00         4.0
#> 7         180        83.00         2.9
#> 8         180        83.00         2.8
#> 9         180        83.00         3.1
#> 10         NA        36.00         4.2
#> 11        180        83.00         3.2
#> 12        180        83.00         3.3
#> 13        180        83.00         3.0
#> 14         NA        36.00         3.0
#> 15        185        83.00         2.8
#> 16        185        83.00         3.0
#> 17        185        83.00         3.2
#> 18        185        83.00         3.3
#> 19        185        83.00         3.1
#> 20        185        83.00         3.5
#> 21        185        83.00         3.4
#> 22        185        83.00         3.6
#> 23        203        83.00         3.6
#> 24        203        83.00         3.7
#> 25        203        83.00         3.5
#> 26        203        83.00         3.8
#> 27        203        83.00         3.9
#> 28        203        83.00         3.4
#> 29        100       160.00         3.4
#> 30        100        83.00         3.5
#> 31        100        83.00         3.4
#> 32        225        83.00         3.6
#> 33        225        83.00         3.5
#> 34        100        83.00         4.0
#> 35        100       160.00         3.5
#> 36        100        83.00         3.7
#> 37        100        83.00         3.3
#> 38        100        83.00         3.2
#> 39        100        83.00         3.6
#> 40        225        83.00         3.3
#> 41        225        83.00         3.4
#> 42        180        83.00         3.6
#> 43         NA        28.00          NA
#> 44        225        83.00         3.2
#> 45        180        83.00         3.4
#> 46        180        83.00         3.5
#> 47        225        83.00         3.7
#> 48        225        83.00         3.8
#> 49        185        83.00         3.8
#> 50        225        83.00         3.9
#> 51        185        83.00         3.7
#> 52         NA        36.00         1.7
#> 53         NA        63.46          NA
#> 54         40       160.00         3.7
#> 55        100       160.00         3.1
#> 56        100       160.00         3.3
#> 57        225        83.00         3.0
#> 58         40       160.00         3.4
#> 59        100       160.00         3.2
#> 60        100       160.00         3.7
#> 61         NA           NA         3.2
#> 62         NA        63.46         2.9
#> 63         NA        63.46         3.0
#> 64         NA           NA         4.1
#> 65         75        63.46         3.1
#> 66         75        63.46         3.4
#> 67         75        63.46         2.9
#> 68         NA        63.46         3.1
#> 69         NA        63.46         2.8
#> 70         75        63.46          NA
#> 71         75        63.46         2.8
#> 72         75        63.46         3.7
#> 73         NA        63.46         3.2
#> 74         NA           NA          NA
#> 75         75        63.46         2.7
#> 76         75       160.00         3.5
#> 77         75       160.00         3.4
#> 78         75       160.00         3.3
#> 79         75       160.00         3.7
#> 80         75       160.00         3.8
#> 81         75       160.00         3.2
#> 82         NA        33.22         3.0
#> 83         75       160.00         3.6
#> 84         NA        33.22         3.2
#> 85         NA        33.22         2.8
#> 86         NA        33.22         3.4
#> 87         NA        33.22          NA
#> 88         NA        63.46         2.7
#> 89         NA        33.22         3.6
#> 90         75        63.46         3.0
#> 91         NA        33.22         3.1
#> 92         NA        33.22         3.3
#> 93         NA        33.22         2.9
#> 94         75        63.46         3.2
#> 95         75        63.46         2.5
#> 96         NA        33.22         3.7
#> 97         NA        33.22         3.5
#> 98         75        63.46         3.3
#> 99         50        63.46         3.0
#> 100        NA        63.46         2.5
#> 101        75        33.22          NA
#> 102        NA        63.46         3.3
#> 103        50        63.46         2.9
#> 104        50        63.46         3.3
#> 105        95        63.46         2.9
#> 106        95        63.46         3.0
#> 107        95        63.46         2.6
#> 108        87        63.46         2.9
#> 109        87        63.46         3.0
#> 110        NA        63.46         3.6
#> 111        NA        63.46         3.5
#> 112        NA        63.46         3.4
#> 113        NA        33.22         1.8
#> 114        NA        63.46         2.6
#> 115        95        63.46         2.8
#> 116        NA        63.46         4.1
#> 117        95        63.46         3.1
#> 118        NA        33.22         2.5
#> 119        NA        33.22         2.7
#> 120        NA        33.22         2.6
#> 121        NA        63.46         2.1
#> 122        NA        63.46         2.0
#> 123        NA        63.46         1.7
#> 124        95        63.46         3.2
#> 125        NA        33.22         3.9
#> 126        75        63.46         2.6
#> 127        NA        33.22         4.9
#> 128        75        63.46         2.4
#> 129       188        83.00         3.3
#> 130       180        83.00         3.7
#> 131       185        83.00         3.9
#> 132       182        83.00         3.3
#> 133       188        83.00         3.2
#> 134       188        83.00         3.6
#> 135       180        83.00         3.8
#> 136       188        83.00         3.4
#> 137       188        83.00         3.9
#> 138       188        83.00         3.7
#> 139       182        83.00         3.7
#> 140        50       160.00         3.0
#> 141        50       160.00         3.2
#> 142        50       160.00         2.9
#> 143        50       160.00         3.3
#> 144        50       160.00         3.1
#> 145        50       160.00         3.8
#> 146        50       160.00         2.8
#> 147        50       160.00         2.7
#> 148        50        63.46          NA
#> 149        50        63.46         3.1
#> 150        NA        33.22         2.1
#> 151        NA        63.46         4.5
#> 152        NA        63.46         3.8
#> 153        NA        63.46         5.4
#> 154        NA        63.46         4.2
#> 155        NA        63.46         4.3
#> 156        NA        63.46         4.6
#> 157        NA        63.46         3.9
#> 158        NA        63.46         3.7
#> 159        NA        63.46         2.2
#> 160        NA        63.46         4.4
#> 161        NA        63.46         4.7
#> 162        NA        63.46         5.6
#> 163        NA        63.46         6.3
#> 164        NA        63.46         5.2
#> 165        NA        63.46         6.0
#> 166        NA        63.46         5.3
#> 167        NA        63.46         6.9
#> 168        NA        63.46         6.2
#> 169        NA        63.46         5.9
#> 170        NA        63.46         7.1
#> 171        NA        63.46         7.3
#> 172        NA        63.46         6.7
#> 173        NA        63.46         0.8
#> 174        NA        63.46         4.9
#> 175        NA        63.46         5.7
#> 176        75        63.46         3.5

# Hmm, Q1 has at least one of the RS or RSA variables as NAs. Will be difficult to standardize!
# Hope the correction factors are present in Ales conversion data

# Now calculate correction factors
hlcodL2 <- hlcodL2 %>% mutate(RS_x = 3/GroundSpeed,
                              Horizontal.opening..m. = Fishing.line*0.67,
                              Swep.one.side..after.formula...meter = 0.258819045*SweepLngt, # SIN(RADIANS(15))
                              Size.final..m = Horizontal.opening..m. + (Swep.one.side..after.formula...meter*2),
                              Swept.area = (Size.final..m*3*1860)/1000000,
                              RSA_x = 0.45388309675081/Swept.area)

hlfleL2 <- hlfleL2 %>% mutate(RS_x = 3/GroundSpeed,
                              Horizontal.opening..m. = Fishing.line*0.67,
                              Swep.one.side..after.formula...meter = 0.258819045*SweepLngt, # SIN(RADIANS(15))
                              Size.final..m = Horizontal.opening..m. + (Swep.one.side..after.formula...meter*2),
                              Swept.area = (Size.final..m*3*1860)/1000000,
                              RSA_x = 0.45388309675081/Swept.area)

# Check EQ. is correct by recalculating it in the sweep file
sweep <- sweep %>% mutate(Horizontal.opening..m.2 = Fishing.line*0.67,
                          Swep.one.side..after.formula...meter2 = 0.258819045*SweepLngt, # SIN(RADIANS(15))
                          Size.final..m2 = Horizontal.opening..m.2 + (Swep.one.side..after.formula...meter2*2),
                          Swept.area2 = (Size.final..m2*3*1860)/1000000,
                          RSA_x = 0.45388309675081/Swept.area2)

sweep %>%
  drop_na() %>%
  ggplot(., aes(as.numeric(RSA), RSA_x)) + geom_point() + geom_abline(intercept = 0, slope = 1)

# Yes it's the same

# Replace NAs with -1/3 (because ICES codes missing values as -9 and in the calculation above they get -1/3),
# so that I can filter them easily later
# sort(unique(hlcodL2$RS_x))
# sort(unique(hlcodL2$RSA_x))

hlcodL2$RS_x[is.na(hlcodL2$RS_x)] <- -1/3
hlcodL2$RS_sweep[is.na(hlcodL2$RS_sweep)] <- -1/3
hlcodL2$RSA_x[is.na(hlcodL2$RSA_x)] <- -1/3
hlcodL2$RSA_sweep[is.na(hlcodL2$RSA_sweep)] <- -1/3

hlfleL2$RS_x[is.na(hlfleL2$RS_x)] <- -1/3
hlfleL2$RS_sweep[is.na(hlfleL2$RS_sweep)] <- -1/3
hlfleL2$RSA_x[is.na(hlfleL2$RSA_x)] <- -1/3
hlfleL2$RSA_sweep[is.na(hlfleL2$RSA_sweep)] <- -1/3

# Compare the difference correction factors (calculated vs imported from sweep file)
p1 <- ggplot(filter(hlcodL2, RS_x > 0), aes(RS_x)) + geom_histogram() + xlim(0.4, 1.76)
p2 <- ggplot(hlcodL2, aes(RSA_x)) + geom_histogram()
p3 <- ggplot(hlcodL2, aes(RS_sweep)) + geom_histogram()
p4 <- ggplot(hlcodL2, aes(RSA_sweep)) + geom_histogram()

(p1 + p2) / (p3 + p4)
#> Warning: Removed 284 rows containing non-finite values (stat_bin).
#> Warning: Removed 2 rows containing missing values (geom_bar).


p5 <- ggplot(filter(hlfleL2, RS_x > 0), aes(RS_x)) + geom_histogram() + xlim(0.4, 1.76)
p6 <- ggplot(hlfleL2, aes(RSA_x)) + geom_histogram()
p7 <- ggplot(hlfleL2, aes(RS_sweep)) + geom_histogram()
p8 <- ggplot(hlfleL2, aes(RSA_sweep)) + geom_histogram()

(p5 + p6) / (p7 + p8)
#> Warning: Removed 115 rows containing non-finite values (stat_bin).
#> Removed 2 rows containing missing values (geom_bar).


# Why do I have RSA values smaller than one? (either because sweep length is longer or gear is larger (GOV))
# Check if I can calculate the same RSA in sweep as that entered there.
# Ok, so the equation is correct. Which ID's have RSA < 1?
hlcodL2 %>% 
  filter(RSA_x < 1 & RSA_x > 0) %>%
  dplyr::select(Year, Country, Ship, Gear, haul.id, Horizontal.opening..m., Fishing.line,
                Swep.one.side..after.formula...meter, SweepLngt, Size.final..m, Swept.area, RSA_x) %>% 
  ggplot(., aes(RSA_x, fill = factor(SweepLngt))) + geom_histogram() + facet_wrap(~Gear, ncol = 1)


# Check if I have more than one unique RS or RSA value per haul, or if it's "either this or that"
# Filter positive in both columns
hlcodL2 %>% filter(RS_x > 0 & RS_sweep > 0) %>% ggplot(., aes(RS_x, RS_sweep)) +
  geom_point() + geom_abline(aes(intercept = 0, slope = 1), color = "red")


hlcodL2 %>% filter(RSA_x > 0 & RSA_sweep > 0) %>% ggplot(., aes(RSA_x, RSA_sweep)) +
  geom_point() + geom_abline(aes(intercept = 0, slope = 1), color = "red")


hlfleL2 %>% filter(RS_x > 0 & RS_sweep > 0) %>% ggplot(., aes(RS_x, RS_sweep)) +
  geom_point() + geom_abline(aes(intercept = 0, slope = 1), color = "red")


hlfleL2 %>% filter(RSA_x > 0 & RSA_sweep > 0) %>% ggplot(., aes(RSA_x, RSA_sweep)) +
  geom_point() + geom_abline(aes(intercept = 0, slope = 1), color = "red")


# Ok, there's on odd RS_x that is larger than 3. It didn't catch anything and speed is 0.8! Will remove
hlcodL2 <- hlcodL2 %>% filter(RS_x < 3)
hlfleL2 <- hlfleL2 %>% filter(RS_x < 3)

# Plot again
hlcodL2 %>% filter(RS_x > 0 & RS_sweep > 0) %>% ggplot(., aes(RS_x, RS_sweep)) +
  geom_point() + geom_abline(aes(intercept = 0, slope = 1), color = "red")


hlfleL2 %>% filter(RS_x > 0 & RS_sweep > 0) %>% ggplot(., aes(RS_x, RS_sweep)) +
  geom_point() + geom_abline(aes(intercept = 0, slope = 1), color = "red")


# They are largely the same when they overlap. When they differ, I will use RS_sweep
# Make a single RS and RSA column

# Cod 
hlcodL3 <- hlcodL2 %>%
  mutate(RS = -99,
         RS = ifelse(RS_sweep > 0, RS_sweep, RS),
         RS = ifelse(RS < 0 & RS_x > 0, RS_x, RS)) %>% # Note that there are no NA i RS_x. This replaces all non-RS_sweep values -0.3, so I can filter positive later
  mutate(RSA = -99,
         RSA = ifelse(RSA_sweep > 0, RSA_sweep, RSA),
         RSA = ifelse(RSA < 0 & RSA_x > 0, RSA_x, RSA)) %>%
  filter(RS > 0) %>%
  filter(RSA > 0) %>% 
  mutate(RSRSA = RS*RSA)

# Plot
ggplot(hlcodL3, aes(RSRSA)) + geom_histogram()


hlfleL2 %>% filter(Country == "LAT") %>% distinct(Year) %>% arrange(Year)
#> # A tibble: 27 × 1
#>     Year
#>    <int>
#>  1  1991
#>  2  1993
#>  3  1995
#>  4  1996
#>  5  1997
#>  6  1999
#>  7  2000
#>  8  2001
#>  9  2002
#> 10  2003
#> # … with 17 more rows

# Flounder 
hlfleL3 <- hlfleL2 %>%
  mutate(RS = -999,
         RS = ifelse(RS_sweep > 0, RS_sweep, RS),
         RS = ifelse(RS < 0, RS_x, RS)) %>% # Note that there are no NA i RS_x. This replaces all non-RS_sweep values -0.3, so I can filter positive later
  mutate(RSA = -999,
         RSA = ifelse(RSA_sweep > 0, RSA_sweep, RSA),
         RSA = ifelse(RSA < 0, RSA_x, RSA)) %>% 
  filter(RS > 0) %>%
  filter(RSA > 0) %>% 
  mutate(RSRSA = RS*RSA)

# Test how many years of LAT data I miss out on because I can't standardize it.
# hlfleL2 %>%
#   mutate(RS = -999,
#          RS = ifelse(RS_sweep > 0, RS_sweep, RS),
#          RS = ifelse(RS < 0, RS_x, RS)) %>% # Note that there are no NA i RS_x. This replaces all non-RS_sweep values -0.3, so I can filter positive later
#   filter(RS > 0) %>% 
#   filter(Country == "LAT") %>% 
#   distinct(Year) %>% 
#   arrange(Year)
#   
# hlfleL2 %>%
#   mutate(RSA = -999,
#          RSA = ifelse(RSA_sweep > 0, RSA_sweep, RSA),
#          RSA = ifelse(RSA < 0, RSA_x, RSA)) %>% 
#   filter(RSA > 0) %>% 
#   filter(Country == "LAT") %>% 
#   distinct(Year) %>% 
#   arrange(Year)

# Plot
ggplot(hlcodL3, aes(RSRSA)) + geom_histogram()


# Standardize!
hlcodL3 <- hlcodL3 %>%
  mutate(CPUEst_kg = CPUEun_kg*RS*RSA,
         CPUEst = CPUEun*RS*RSA)

hlfleL3 <- hlfleL3 %>%
  mutate(CPUEst_kg = CPUEun_kg*RS*RSA,
         CPUEst = CPUEun*RS*RSA)
  
unique(is.na(hlcodL3$CPUEst_kg))
#> [1] FALSE
unique(is.na(hlcodL3$CPUEst))
#> [1] FALSE
min(hlcodL3$CPUEst_kg)
#> [1] 0
min(hlcodL3$CPUEst)
#> [1] 0

unique(is.na(hlfleL3$CPUEst_kg)) # Remove the few NA's here
#> [1]  TRUE FALSE
hlfleL3 <- hlfleL3 %>% drop_na(CPUEst_kg)
unique(is.na(hlfleL3$CPUEst))
#> [1] FALSE
min(hlfleL3$CPUEst_kg) 
#> [1] 0
min(hlfleL3$CPUEst)
#> [1] 0

# Now calculate total CPUE per haul in Orios unit, then create the new unit, i.e.:
# convert from kg of fish caught by trawling for 1 h a standard bottom swept area of
# 0.45km2 (using a TVL trawl with 75 m sweeps at the standard speed of three knots) to....
# kg of fish per km^2 by dividing with 0.45

hlcodhaul <- hlcodL3 %>%
  group_by(haul.id) %>% 
  mutate(haul_cpue_kg = sum(CPUEst_kg),
         haul_cpue = sum(CPUEst),
         haul_cpue_kg_un = sum(CPUEun_kg),
         haul_cpue_un = sum(CPUEun),
         density = haul_cpue_kg/0.45,
         density_ab = haul_cpue/0.45) %>% 
  ungroup() %>% 
  distinct(haul.id, .keep_all = TRUE)

# t <- hlcodhaul %>% filter(haul_cpue_un == 0)
# t <- hlcodhaul %>% filter(!Country == "SWE") %>% filter(haul_cpue_un > 0)

hlflehaul <- hlfleL3 %>%
  group_by(haul.id) %>% 
  mutate(haul_cpue_kg = sum(CPUEst_kg),
         haul_cpue = sum(CPUEst),
         haul_cpue_kg_un = sum(CPUEun_kg),
         haul_cpue_un = sum(CPUEun),
         density = haul_cpue_kg/0.45,
         density_ab = haul_cpue/0.45) %>% 
  ungroup() %>% 
  distinct(haul.id, .keep_all = TRUE)

# Rename things and select specific columns
dat <- hlcodhaul %>% rename("year" = "Year",
                            "lat" = "ShootLat",
                            "lon" = "ShootLong",
                            "quarter" = "Quarter",
                            "ices_rect" = "Rect",
                            "depth" = "Depth") %>% 
  dplyr::select(density, year, lat, lon, quarter, haul.id, IDx, ices_rect, sub_div, depth)

# Now add in the flounder data in the cod data based on haul ID
hlflehaul_select <- hlflehaul %>% mutate(density_fle = density) %>% dplyr::select(density_fle, haul.id)

dat <- left_join(dat, hlflehaul_select, by = "haul.id") %>% drop_na(density_fle)

dat %>% arrange(desc(density_fle)) %>% data.frame() %>% head(50)
#>        density year     lat     lon quarter                     haul.id
#> 1    84.958409 2008 57.1850 20.6817       1     2008:1:LAT:BALL:TVL:1:4
#> 2  3107.363608 2008 56.2717 19.6533       1    2008:1:LAT:BALL:TVL:1:27
#> 3    45.808880 2007 56.9917 20.8033       1     2007:1:LAT:BALL:TVL:1:4
#> 4    74.676369 2010 57.3617 21.2350       4     2010:4:LAT:BALL:TVL:2:6
#> 5   295.261272 2012 56.5000 20.2150       1    2012:1:LAT:BALL:TVL:1:24
#> 6   102.464396 2011 56.9533 20.4150       1    2011:1:LAT:BALL:TVL:1:16
#> 7   242.575119 2008 57.2067 20.7283       1     2008:1:LAT:BALL:TVL:1:5
#> 8    13.872224 2010 57.2217 20.7167       1    2010:1:LAT:BALL:TVL:1:17
#> 9    51.843861 2007 55.7683 20.3333       1     2007:1:LAT:BALL:TVL:1:1
#> 10   95.664659 2007 57.2124 18.8491       4   2007:4:SWE:ARG:TVL:689:14
#> 11    6.508112 2009 57.8969 19.4288       1    2009:1:SWE:ARG:TVL:211:9
#> 12 6897.378835 2009 55.1083 19.7017       1     2009:1:RUS:ATL:TVL:57:4
#> 13   35.005848 2008 56.9350 20.3833       1     2008:1:LAT:BALL:TVL:1:1
#> 14  262.944923 2012 57.4600 21.2450       4     2012:4:LAT:BALL:TVL:2:6
#> 15  149.309819 2019 54.4500 19.3283       4  2019:4:POL:BAL:TVL:26256:4
#> 16   36.859878 2009 57.8994 19.4332       4   2009:4:SWE:ARG:TVL:723:11
#> 17   24.702776 2010 57.3550 20.5900       1    2010:1:LAT:BALL:TVL:1:15
#> 18 1153.634376 2014 55.4587 14.7253       1   2014:1:SWE:DAN2:TVL:48:26
#> 19  109.008774 2012 57.3683 21.2533       4     2012:4:LAT:BALL:TVL:2:7
#> 20  352.897387 2017 55.0233 16.3233       1 2017:1:POL:BAL:TVL:25066:28
#> 21 3499.177768 2011 55.3583 19.9267       1   2011:1:RUS:ATLD:TVL:56:40
#> 22  687.181689 2007 54.8692 15.4500       1 2007:1:POL:BAL:TVL:25159:31
#> 23    0.000000 2007 54.3692 19.2192       1 2007:1:POL:BAL:TVL:26001:35
#> 24  740.323341 2009 55.4950 20.0950       1    2009:1:RUS:ATL:TVL:57:10
#> 25  158.671747 1992 55.5833 15.8833       1 1992:1:GFR:SOL:CHP:25081:21
#> 26   31.348817 2001 57.1830 18.8793       1   2001:1:SWE:ARG:TVL:230:24
#> 27 3062.002553 2020 55.2367 17.3000       1  2020:1:POL:BAL:TVL:25463:9
#> 28  987.921853 2013 55.0750 19.6117       1   2013:1:RUS:ATLD:TVL:60:22
#> 29   53.784153 2007 56.5967 20.5100       1    2007:1:LAT:BALL:TVL:1:28
#> 30 4435.725757 2018 54.8317 19.6633       1     2018:1:RUS:ATL:TVL:67:2
#> 31 1838.109258 2001 56.4709 18.6149       4   2001:4:DEN:DAN2:TVL:93:38
#> 32 2176.245400 2020 55.2567 17.3700       1 2020:1:POL:BAL:TVL:25339:14
#> 33   62.375164 2020 54.4217 19.0267       1 2020:1:POL:BAL:TVL:26264:19
#> 34  181.453707 2008 57.1766 18.8081       4   2008:4:SWE:ARG:TVL:697:12
#> 35  883.537784 2019 55.4627 14.5290       4    2019:4:SWE:77SE:TVL:66:2
#> 36 1781.872489 2001 57.3537 19.1588       1   2001:1:SWE:ARG:TVL:236:29
#> 37 7210.671879 2014 55.4928 14.6408       1   2014:1:SWE:DAN2:TVL:51:27
#> 38  463.644551 2011 57.2433 21.0817       4    2011:4:LAT:BALL:TVL:2:11
#> 39  533.357067 1995 54.7667 15.6500       1   1995:1:GFR:SOL:H20:74:105
#> 40  274.848183 2001 55.0657 16.2425       1   2001:1:DEN:DAN2:TVL:81:39
#> 41  322.590582 2008 57.4733 21.1167       4     2008:4:LAT:BALL:TVL:2:3
#> 42  417.838920 2001 55.0489 16.2850       1   2001:1:DEN:DAN2:TVL:79:38
#> 43  511.328391 2016 54.7683 15.3100       1 2016:1:POL:BAL:TVL:25177:12
#> 44   24.325399 2007 57.2217 20.7467       1    2007:1:LAT:BALL:TVL:1:18
#> 45   22.230389 2011 57.0000 20.8117       1     2011:1:LAT:BALL:TVL:1:7
#> 46 1111.835196 2002 54.8461 15.3151       1   2002:1:DEN:DAN2:TVL:32:18
#> 47    1.255573 2008 57.0667 20.6367       1    2008:1:LAT:BALL:TVL:1:16
#> 48    9.415983 2013 57.7022 19.1408       4   2013:4:SWE:DAN2:TVL:33:17
#> 49 1642.707733 2007 57.2033 20.7167       4     2007:4:LAT:BALL:TVL:2:9
#> 50 1335.057913 2010 54.8850 18.8750       1 2010:1:POL:BAL:TVL:26087:33
#>                             IDx ices_rect sub_div depth density_fle
#> 1       2008.1.LAT.67BC.TVL.1.4      43H0      28    83   11169.594
#> 2      2008.1.LAT.67BC.TVL.1.27      41G9      26    73    9046.180
#> 3       2007.1.LAT.67BC.TVL.1.4      42H0      28    64    6636.965
#> 4       2010.4.LAT.67BC.TVL.2.6      43H1      28    64    6434.487
#> 5      2012.1.LAT.67BC.TVL.1.24      42H0      28    86    5782.487
#> 6      2011.1.LAT.67BC.TVL.1.16      42H0      28    86    5327.548
#> 7       2008.1.LAT.67BC.TVL.1.5      43H0      28    77    4178.697
#> 8      2010.1.LAT.67BC.TVL.1.17      43H0      28    81    3942.139
#> 9       2007.1.LAT.67BC.TVL.1.1      40H0      26    55    3200.816
#> 10   2007.4.SWE.77AR.TVL.689.14      43G8      28    43    3196.460
#> 11    2009.1.SWE.77AR.TVL.211.9      44G9      28    48    2807.984
#> 12     2009.1.RUS.RUNT.TVL.57.4      39G9      26    80    2712.294
#> 13      2008.1.LAT.67BC.TVL.1.1      42H0      28    80    2707.929
#> 14      2012.4.LAT.67BC.TVL.2.6      43H1      28    66    2680.423
#> 15  2019.4.POL.67BC.TVL.26256.4      37G9      26    62    2505.173
#> 16   2009.4.SWE.77AR.TVL.723.11      44G9      28    48    2481.592
#> 17     2010.1.LAT.67BC.TVL.1.15      43H0      28    87    2447.802
#> 18    2014.1.SWE.26D4.TVL.48.26      39G4      24    68    2442.102
#> 19      2012.4.LAT.67BC.TVL.2.7      43H1      28    55    2424.936
#> 20 2017.1.POL.67BC.TVL.25066.28      39G6      25    56    2416.610
#> 21    2011.1.RUS.RUJB.TVL.56.40      39G9      26    82    2415.328
#> 22 2007.1.POL.67BC.TVL.25159.31      38G5      25    77    2381.793
#> 23 2007.1.POL.67BC.TVL.26001.35      37G9      26    23    2357.677
#> 24    2009.1.RUS.RUNT.TVL.57.10      39H0      26    75    2341.579
#> 25 1992.1.GFR.06S1.CHP.25081.21      40G5      25    81    2286.580
#> 26   2001.1.SWE.77AR.TVL.230.24      43G8      28    53    2207.518
#> 27  2020.1.POL.67BC.TVL.25463.9      39G7      25    91    2206.927
#> 28    2013.1.RUS.RUJB.TVL.60.22      39G9      26    87    2188.610
#> 29     2007.1.LAT.67BC.TVL.1.28      42H0      28    64    2144.123
#> 30     2018.1.RUS.RUNT.TVL.67.2      38G9      26    60    2128.009
#> 31    2001.4.DEN.26D4.TVL.93.38      41G8      26    44    2098.998
#> 32 2020.1.POL.67BC.TVL.25339.14      39G7      25    88    2070.693
#> 33 2020.1.POL.67BC.TVL.26264.19      37G9      26    42    2063.225
#> 34   2008.4.SWE.77AR.TVL.697.12      43G8      28    40    2048.861
#> 35     2019.4.SWE.77SE.TVL.66.2      39G4      24    58    2038.147
#> 36   2001.1.SWE.77AR.TVL.236.29      43G9      28    81    2019.608
#> 37    2014.1.SWE.26D4.TVL.51.27      39G4      24    60    1999.449
#> 38     2011.4.LAT.67BC.TVL.2.11      43H1      28    61    1996.229
#> 39   1995.1.GFR.06S1.H20.74.105      38G5      25    78    1963.572
#> 40    2001.1.DEN.26D4.TVL.81.39      39G6      25    77    1909.892
#> 41      2008.4.LAT.67BC.TVL.2.3      43H1      28    71    1893.857
#> 42    2001.1.DEN.26D4.TVL.79.38      39G6      25    72    1870.911
#> 43 2016.1.POL.67BC.TVL.25177.12      38G5      25    68    1869.831
#> 44     2007.1.LAT.67BC.TVL.1.18      43H0      28    58    1829.415
#> 45      2011.1.LAT.67BC.TVL.1.7      43H0      28    57    1796.272
#> 46    2002.1.DEN.26D4.TVL.32.18      38G5      25    73    1765.521
#> 47     2008.1.LAT.67BC.TVL.1.16      43H0      28    88    1757.879
#> 48    2013.4.SWE.26D4.TVL.33.17      44G9      28    40    1696.887
#> 49      2007.4.LAT.67BC.TVL.2.9      43H0      28    75    1695.008
#> 50 2010.1.POL.67BC.TVL.26087.33      38G8      26   101    1692.796

# Filter unrealistically large densities 
dat <- dat %>% filter(density_fle < 10000)

ggplot(dat, aes(density, density_fle)) + geom_point()

Compare cod data with Orio et al (2017)

# Read Orio data first for comparison:
test_cod <- hlcodhaul %>% filter(!Country == "SWE")
test_fle <- hlflehaul %>% filter(!Country == "SWE")

orio_cod <- read.csv("/Users/maxlindmark/Dropbox/Max work/R/gear-standardization-ale/datras_st_ale.csv")
orio_fle <- read.csv("/Users/maxlindmark/Dropbox/Max work/R/gear-standardization-ale/datras_fle_st_ale.csv")

orio_cod <- orio_cod %>%
  group_by(IDX) %>%
  mutate(haul_cpue_kg = sum(CPUEstBIOyear),
         haul_cpue = sum(CPUEst),
         haul_cpue_kg_un = sum(CPUEunBIOyear),
         haul_cpue_un = sum(CPUEun)) %>%
  ungroup() %>%
  distinct(IDX, .keep_all = TRUE)

orio_fle <- orio_fle %>%
  group_by(IDX) %>%
  mutate(haul_cpue_kg = sum(CPUEstBIOyear),
         haul_cpue = sum(CPUEst),
         haul_cpue_kg_un = sum(CPUEunBIOyear),
         haul_cpue_un = sum(CPUEun)) %>%
  ungroup() %>%
  distinct(IDX, .keep_all = TRUE)

# hlcodhaul %>% group_by(IDx) %>% mutate(n = n()) %>% ungroup() %>% distinct(n)

# Standardize data for easier plotting
test_cod_q1 <- test_cod %>%
  dplyr::select(haul_cpue_kg, haul_cpue, haul_cpue_kg_un, haul_cpue_un, Year, Ship3, Country, Gear, Quarter) %>% 
  mutate(source = "Max",
         species = "Cod") %>% 
  rename("Ship" = "Ship3") %>% 
  filter(Year > 1992 & Year < 2017 & Quarter == 1)

test_cod_q4 <- test_cod %>%
  dplyr::select(haul_cpue_kg, haul_cpue, haul_cpue_kg_un, haul_cpue_un, Year, Ship3, Country, Gear, Quarter) %>% 
  mutate(source = "Max",
         species = "Cod") %>% 
  rename("Ship" = "Ship3") %>% 
  filter(Year > 1992 & Year < 2017 & Quarter == 4)

test_fle_q1 <- test_fle %>%
  dplyr::select(haul_cpue_kg, haul_cpue, haul_cpue_kg_un, haul_cpue_un, Year, Ship3, Country, Gear, Quarter) %>% 
  mutate(source = "Max",
         species = "Fle") %>% 
  rename("Ship" = "Ship3") %>% 
  filter(Year > 1992 & Year < 2017 & Quarter == 1)

test_fle_q4 <- test_fle %>%
  dplyr::select(haul_cpue_kg, haul_cpue, haul_cpue_kg_un, haul_cpue_un, Year, Ship3, Country, Gear, Quarter) %>% 
  mutate(source = "Max",
         species = "Fle") %>% 
  rename("Ship" = "Ship3") %>% 
  filter(Year > 1992 & Year < 2017 & Quarter == 4)

orio_cod_q1 <- orio_cod %>%
  dplyr::select(haul_cpue_kg, haul_cpue, haul_cpue_kg_un, haul_cpue_un, year, vessel, IDX) %>% 
  mutate(source = "Ale",
         species = "Cod") %>% 
  rename("Year" = "year",
         "Ship" = "vessel") %>% 
  mutate(IDX2 = IDX,
         IDX3 = IDX) %>% 
  separate(IDX, sep = c(5:7), into = c("temp_year", "Quarter")) %>% 
  separate(temp_year, sep = c(4:5), into = c("Year", "scrap")) %>% 
  separate(IDX2, sep = 7, into = c("scrap2", "Country")) %>%
  separate(Country, sep = 3, into = c("Country", "scrap3")) %>%
  separate(IDX3, sep = 15, into = c("scrap4", "Gear")) %>%
  separate(Gear, sep = 3, into = c("Gear", "scrap5")) %>% 
  dplyr::select(-scrap, -scrap2, -scrap3, -scrap4, -scrap5) %>% 
  filter(Quarter == 1) %>% 
  mutate(Year = as.integer(Year),
         Quarter = as.integer(Quarter)) %>% 
  mutate(haul_cpue_kg = haul_cpue_kg/1000,
         haul_cpue_kg_un = haul_cpue_kg_un/1000) %>% 
  filter(Year > 1992) %>% 
  filter(Country %in% unique(test_cod$Country))

orio_cod_q4 <- orio_cod %>%
  dplyr::select(haul_cpue_kg, haul_cpue, haul_cpue_kg_un, haul_cpue_un, year, vessel, IDX) %>% 
  mutate(source = "Ale",
         species = "Cod") %>% 
  rename("Year" = "year",
         "Ship" = "vessel") %>% 
  mutate(IDX2 = IDX,
         IDX3 = IDX) %>% 
  separate(IDX, sep = c(5:7), into = c("temp_year", "Quarter")) %>% 
  separate(temp_year, sep = c(4:5), into = c("Year", "scrap")) %>% 
  separate(IDX2, sep = 7, into = c("scrap2", "Country")) %>%
  separate(Country, sep = 3, into = c("Country", "scrap3")) %>%
  separate(IDX3, sep = 15, into = c("scrap4", "Gear")) %>%
  separate(Gear, sep = 3, into = c("Gear", "scrap5")) %>% 
  dplyr::select(-scrap, -scrap2, -scrap3, -scrap4, -scrap5) %>% 
  filter(Quarter == 4) %>% 
  mutate(Year = as.integer(Year),
         Quarter = as.integer(Quarter)) %>% 
  mutate(haul_cpue_kg = haul_cpue_kg/1000,
         haul_cpue_kg_un = haul_cpue_kg_un/1000) %>% 
  filter(Year > 1992) %>% 
  filter(Country %in% unique(test_cod$Country))

orio_fle_q1 <- orio_fle %>% 
  dplyr::select(haul_cpue_kg, haul_cpue, haul_cpue_kg_un, haul_cpue_un, year, vessel, IDX) %>% 
  mutate(source = "Ale",
         species = "Fle") %>% 
  rename("Year" = "year",
         "Ship" = "vessel") %>% 
  mutate(IDX2 = IDX,
         IDX3 = IDX) %>% 
  separate(IDX, sep = c(5:7), into = c("temp_year", "Quarter")) %>% 
  separate(temp_year, sep = c(4:5), into = c("Year", "scrap")) %>% 
  separate(IDX2, sep = 7, into = c("scrap2", "Country")) %>%
  separate(Country, sep = 3, into = c("Country", "scrap3")) %>%
  separate(IDX3, sep = 15, into = c("scrap4", "Gear")) %>%
  separate(Gear, sep = 3, into = c("Gear", "scrap5")) %>% 
  dplyr::select(-scrap, -scrap2, -scrap3, -scrap4, -scrap5) %>% 
  filter(Quarter == 1) %>% 
  mutate(Year = as.integer(Year),
         Quarter = as.integer(Quarter)) %>% 
  mutate(haul_cpue_kg = haul_cpue_kg/1000,
         haul_cpue_kg_un = haul_cpue_kg_un/1000) %>% 
  filter(Year > 1992) %>% 
  filter(Country %in% unique(test_cod$Country))

orio_fle_q4 <- orio_fle %>% 
  dplyr::select(haul_cpue_kg, haul_cpue, haul_cpue_kg_un, haul_cpue_un, year, vessel, IDX) %>% 
  mutate(source = "Ale",
         species = "Fle") %>% 
  rename("Year" = "year",
         "Ship" = "vessel") %>% 
  mutate(IDX2 = IDX,
         IDX3 = IDX) %>% 
  separate(IDX, sep = c(5:7), into = c("temp_year", "Quarter")) %>% 
  separate(temp_year, sep = c(4:5), into = c("Year", "scrap")) %>% 
  separate(IDX2, sep = 7, into = c("scrap2", "Country")) %>%
  separate(Country, sep = 3, into = c("Country", "scrap3")) %>%
  separate(IDX3, sep = 15, into = c("scrap4", "Gear")) %>%
  separate(Gear, sep = 3, into = c("Gear", "scrap5")) %>% 
  dplyr::select(-scrap, -scrap2, -scrap3, -scrap4, -scrap5) %>% 
  filter(Quarter == 4) %>% 
  mutate(Year = as.integer(Year),
         Quarter = as.integer(Quarter)) %>% 
  mutate(haul_cpue_kg = haul_cpue_kg/1000,
         haul_cpue_kg_un = haul_cpue_kg_un/1000) %>% 
  filter(Year > 1992) %>% 
  filter(Country %in% unique(test_cod$Country))

sort(unique(test_cod_q1$Country))
#> [1] "DEN" "GFR" "LAT" "LTU" "POL" "RUS"
sort(unique(orio_cod_q1$Country))
#> [1] "DEN" "GFR" "LAT" "LTU" "POL" "RUS"

# Check proportions of zero
t <- test_cod_q1 %>% filter(haul_cpue_kg > 0)
t <- orio_cod_q1 %>% filter(haul_cpue_kg > 0)

t <- test_cod_q4 %>% filter(haul_cpue_kg > 0)
t <- orio_cod_q4 %>% filter(haul_cpue_kg > 0)

t <- test_fle_q1 %>% filter(haul_cpue_kg > 0)
t <- orio_fle_q1 %>% filter(haul_cpue_kg > 0)

t <- test_fle_q4 %>% filter(haul_cpue_kg > 0)
t <- orio_fle_q4 %>% filter(haul_cpue_kg > 0)

test_full <- bind_rows(orio_fle_q1, orio_fle_q4, orio_cod_q1, orio_cod_q4,
                       test_cod_q1, test_cod_q4, test_fle_q1, test_fle_q4)

# Check the non-standardized data for cod
# Raw abundance cpue
test_full %>% 
  filter(species == "Cod") %>% 
  ggplot(., aes(factor(Year), haul_cpue_un, color = source)) + 
  geom_point(size = 1, position = position_dodge(width = 0.5)) + 
  theme(axis.text.x = element_text(angle = 90)) +
  facet_wrap(~Quarter)

  
test_full %>% 
  filter(species == "Cod" & Quarter == 1) %>% 
  ggplot(., aes(haul_cpue_un, fill = source)) + 
  geom_histogram(position = position_dodge()) + 
  facet_wrap(~Year, scale = "free") +
  theme(axis.text.x = element_text(angle = 90))


test_full %>% 
  filter(species == "Cod" & Quarter == 4) %>% 
  ggplot(., aes(haul_cpue_un, fill = source)) + 
  geom_histogram(position = position_dodge()) + 
  facet_wrap(~Year, scale = "free") +
  theme(axis.text.x = element_text(angle = 90))


# Mean abundance cpue
test_full %>% 
  filter(species == "Cod") %>% 
  group_by(Year, Quarter, source) %>% 
  summarise(mean_cpue = mean(haul_cpue_un),
            sd_cpue = sd(haul_cpue_un)) %>% 
  ggplot(., aes(Year, mean_cpue, linetype = source, color = source)) + 
  geom_line(size = 1.1) +
  geom_point(size = 3) + 
  facet_wrap(~Quarter, ncol = 1)


# Mean abundance cpue by country
test_full %>% 
  filter(species == "Cod") %>% 
  group_by(Year, source, Quarter, Country) %>% 
  summarise(mean_cpue = mean(haul_cpue_un),
            sd_cpue = sd(haul_cpue_un)) %>% 
  ggplot(., aes(Year, mean_cpue, linetype = source, color = source)) + 
  geom_line(size = 1.1) +
  facet_grid(Quarter ~ Country)


# Mean abundance cpue for non-zero cathes
test_full %>% 
  filter(species == "Cod") %>% 
  filter(haul_cpue_un > 0) %>% 
  group_by(Year, Quarter, source) %>% 
  summarise(mean_cpue = mean(haul_cpue_un),
            sd_cpue = sd(haul_cpue_un)) %>% 
  ggplot(., aes(Year, mean_cpue, linetype = source, color = source)) + 
  geom_line(size = 1.1) +
  geom_point(size = 3) + 
  facet_wrap(~ Quarter, ncol = 1)


# Now plot corrected biomass cpue
test_full %>% 
  filter(species == "Cod") %>% 
  ggplot(., aes(factor(Year), haul_cpue_kg, color = source)) + 
  geom_point(size = 1, position = position_dodge(width = 0.5)) + 
  theme(axis.text.x = element_text(angle = 90)) +
  facet_wrap(~ Quarter, ncol = 1)


# Mean corrected biomass cpue
test_full %>% 
  filter(species == "Cod") %>% 
  group_by(Year, Quarter, source) %>% 
  summarise(mean_cpue = mean(haul_cpue_kg),
            sd_cpue = sd(haul_cpue_kg)) %>% 
  ggplot(., aes(Year, mean_cpue, linetype = source, color = source)) + 
  geom_line(size = 1.1) +
  geom_point(size = 3) + 
  facet_wrap(~ Quarter, ncol = 1)


# Now check in on flounder
# Raw abundance cpue
test_full %>% 
  filter(species == "Fle") %>% 
  ggplot(., aes(factor(Year), haul_cpue_un, color = source)) + 
  geom_point(size = 1, position = position_dodge(width = 0.5)) + 
  theme(axis.text.x = element_text(angle = 90)) +
  facet_wrap(~ Quarter, ncol = 1)

  
test_full %>% 
  filter(species == "Fle" & Quarter == 1) %>% 
  ggplot(., aes(haul_cpue_un, fill = source)) + 
  geom_histogram(position = position_dodge()) + 
  facet_wrap(~Year, scale = "free") +
  theme(axis.text.x = element_text(angle = 90))


test_full %>% 
  filter(species == "Fle" & Quarter == 4) %>% 
  ggplot(., aes(haul_cpue_un, fill = source)) + 
  geom_histogram(position = position_dodge()) + 
  facet_wrap(~Year, scale = "free") +
  theme(axis.text.x = element_text(angle = 90))


# Mean abundance cpue
test_full %>% 
  filter(species == "Fle") %>% 
  group_by(Year, Quarter, source) %>% 
  summarise(mean_cpue = mean(haul_cpue_un),
            sd_cpue = sd(haul_cpue_un)) %>% 
  ggplot(., aes(Year, mean_cpue, linetype = source, color = source)) + 
  geom_line(size = 1.1) +
  geom_point(size = 3) + 
  facet_wrap(~ Quarter, ncol = 1)


# Ok, here we can see that Ale predicts an increase earlier, which we also saw 
# on the plot of raw catches as points
# Mean abundance cpue by country to see how this can arise
test_full %>% 
  filter(species == "Fle") %>% 
  group_by(Year, Quarter, source, Country) %>% 
  summarise(mean_cpue = mean(haul_cpue_un),
            sd_cpue = sd(haul_cpue_un)) %>% 
  ggplot(., aes(Year, mean_cpue, linetype = source, color = source)) + 
  geom_line(size = 1.1) +
  facet_grid(Quarter ~ Country)


# Ok, after checking, the reason I don't have the older LAT data is because I don't
# have RS or RSA-values for those hauls, even though the catch data are in datras...
# Should probably check with Ale how he corrected those!

# Now plot corrected biomass cpue
test_full %>% 
  filter(species == "Fle") %>% 
  ggplot(., aes(factor(Year), haul_cpue_kg, color = source)) + 
  geom_point(size = 1, position = position_dodge(width = 0.5)) + 
  theme(axis.text.x = element_text(angle = 90)) +
  facet_wrap(~ Quarter, ncol = 1)


# Mean corrected biomass cpue
test_full %>% 
  filter(species == "Fle") %>% 
  group_by(Year, Quarter, source) %>% 
  summarise(mean_cpue = mean(haul_cpue_kg),
            sd_cpue = sd(haul_cpue_kg)) %>% 
  ggplot(., aes(Year, mean_cpue, linetype = source, color = source)) + 
  geom_line(size = 1.1) +
  geom_point(size = 3) + 
  facet_wrap(~ Quarter, ncol = 1)


# Then check length-weight relationships again

Add in the environmental variables

Depth

# Read the tifs
west <- raster("data/depth_geo_tif/D5_2018_rgb-1.tif")
#plot(west)

east <- raster("data/depth_geo_tif/D6_2018_rgb-1.tif")
# plot(east)

dep_rast <- raster::merge(west, east)

dat$depth_r <- extract(dep_rast, dat %>% dplyr::select(lon, lat), method = "bilinear")

# Convert to depth (instead of elevation)
ggplot(dat, aes(depth)) + geom_histogram()
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> Warning: Removed 1 rows containing non-finite values (stat_bin).


dat$depth_r <- (dat$depth_r - max(drop_na(dat)$depth_r)) *-1
#> drop_na: removed one row (<1%), 8,938 rows remaining

ggplot(dat, aes(depth_r)) + geom_histogram()
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(dat, aes(depth)) + geom_histogram()
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> Warning: Removed 1 rows containing non-finite values (stat_bin).

ggplot(dat, aes(depth_r, depth)) + geom_point() + geom_abline(color = "red")
#> Warning: Removed 1 rows containing missing values (geom_point).


ggplot(dat, aes(depth_r, depth)) +
  geom_point() +
  geom_abline(color = "red") +
  coord_cartesian(xlim = c(0, 20), ylim = c(0, 75)) + 
  geom_vline(xintercept = 10, color = "blue") +
  geom_hline(yintercept = 10, color = "blue")
#> Warning: Removed 1 rows containing missing values (geom_point).


ggplot(dat, aes(depth_r, depth)) +
  geom_point() +
  geom_abline(color = "red") +
  coord_cartesian(xlim = c(90, 130), ylim = c(75, 125)) + 
  geom_vline(xintercept = 110, color = "blue") +
  geom_hline(yintercept = 110, color = "blue")
#> Warning: Removed 1 rows containing missing values (geom_point).


# So, roughly 3 meter is 10, and 120 is 110

summary(dat$depth)
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
#>   10.00   40.00   54.00   55.12   71.00  190.00       1
quantile(drop_na(dat)$depth, prob = c(0.01, 0.99))
#> drop_na: removed one row (<1%), 8,938 rows remaining
#>  1% 99% 
#>  16 108
summary(dat$depth_r)
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>    0.00   25.31   43.89   46.81   66.00  165.47

dat <- dat %>% 
  dplyr::select(-depth) %>% 
  rename(depth = depth_r)
#> rename: renamed one variable (depth)

Oxygen

# Rename fishdata to fishdat so that I don't overwrite anything. Not all years in the BITS
# have environmental data, and I don't want to loose important trawl information 

fishdat <- dat

# Downloaded from here: https://resources.marine.copernicus.eu/?option=com_csw&view=details&product_id=BALTICSEA_REANALYSIS_BIO_003_012
# Extract raster points: https://gisday.wordpress.com/2014/03/24/extract-raster-values-from-points-using-r/comment-page-1/
# https://rpubs.com/boyerag/297592
# https://pjbartlein.github.io/REarthSysSci/netCDF.html#get-a-variable
# Open the netCDF file
ncin <- nc_open("data/NEMO_Nordic_SCOBI/dataset-reanalysis-scobi-monthlymeans_1610091357600.nc")

print(ncin)
#> File data/NEMO_Nordic_SCOBI/dataset-reanalysis-scobi-monthlymeans_1610091357600.nc (NC_FORMAT_CLASSIC):
#> 
#>      1 variables (excluding dimension variables):
#>         float o2b[longitude,latitude,time]   
#>             long_name: Sea_floor_Dissolved_Oxygen_Concentration
#>             missing_value: NaN
#>             standard_name: mole_concentration_of_dissolved_molecular_oxygen_in_sea_water
#>             units: mmol m-3
#>             _FillValue: NaN
#>             _ChunkSizes: 1
#>              _ChunkSizes: 523
#>              _ChunkSizes: 383
#> 
#>      3 dimensions:
#>         time  Size:324
#>             axis: T
#>             long_name: Validity time
#>             standard_name: time
#>             units: days since 1950-01-01 00:00:00
#>             calendar: gregorian
#>             _ChunkSizes: 512
#>             _CoordinateAxisType: Time
#>             valid_min: 15721.5
#>             valid_max: 25551.5
#>         latitude  Size:166
#>             axis: Y
#>             standard_name: latitude
#>             long_name: latitude
#>             units: degrees_north
#>             _CoordinateAxisType: Lat
#>             valid_min: 52.991626739502
#>             valid_max: 58.4915390014648
#>         longitude  Size:253
#>             standard_name: longitude
#>             long_name: longitude
#>             units: degrees_east
#>             axis: X
#>             _CoordinateAxisType: Lon
#>             valid_min: 9.01375484466553
#>             valid_max: 23.013614654541
#> 
#>     24 global attributes:
#>         references: http://www.smhi.se
#>         institution: Swedish Meterological and Hydrological Institute
#>         history: See source and creation_date attributees
#>         Conventions: CF-1.5
#>         contact: servicedesk_cmems@mercator-ocean.eu
#>         comment: Provided by SMHI as a Copernicus Marine Environment Monitoring Service production unit
#>         bullentin_type: reanalysis
#>         cmems_product_id: BALTICSEA_REANALYSIS_BIO_003_012
#>         title: CMEMS V4 Reanalysis: SCOBI model 3D fields (monthly means)
#>         FROM_ORIGINAL_FILE__easternmost_longitude: 30.2357654571533
#>         FROM_ORIGINAL_FILE__northernmost_latitude: 65.8914184570312
#>         FROM_ORIGINAL_FILE__westernmost_longitude: 9.01375484466553
#>         FROM_ORIGINAL_FILE__southernmost_latitude: 48.49169921875
#>         shallowest_depth: 1.50136542320251
#>         deepest_depth: 711.059204101562
#>         source: SMHI reanalysis run NORDIC-NS2_1d_20191201_20191202
#>         file_quality_index: 1
#>         creation_date: 2020-11-16 UTC
#>         bullentin_date: 20191201
#>         start_date: 2019-12-01 UTC
#>         stop_date: 2019-12-01 UTC
#>         start_time: 00:00 UTC
#>         stop_time: 00:00 UTC
#>         _CoordSysBuilder: ucar.nc2.dataset.conv.CF1Convention

# Get longitude and latitude
lon <- ncvar_get(ncin,"longitude")
nlon <- dim(lon)
head(lon)
#> [1] 9.013755 9.069310 9.124865 9.180420 9.235975 9.291530

lat <- ncvar_get(ncin,"latitude")
nlat <- dim(lat)
head(lat)
#> [1] 52.99163 53.02496 53.05829 53.09163 53.12496 53.15829

# Get time
time <- ncvar_get(ncin,"time")
time
#>   [1] 15721.5 15751.0 15780.5 15811.0 15841.5 15872.0 15902.5 15933.5 15964.0
#>  [10] 15994.5 16025.0 16055.5 16086.5 16116.0 16145.5 16176.0 16206.5 16237.0
#>  [19] 16267.5 16298.5 16329.0 16359.5 16390.0 16420.5 16451.5 16481.0 16510.5
#>  [28] 16541.0 16571.5 16602.0 16632.5 16663.5 16694.0 16724.5 16755.0 16785.5
#>  [37] 16816.5 16846.5 16876.5 16907.0 16937.5 16968.0 16998.5 17029.5 17060.0
#>  [46] 17090.5 17121.0 17151.5 17182.5 17212.0 17241.5 17272.0 17302.5 17333.0
#>  [55] 17363.5 17394.5 17425.0 17455.5 17486.0 17516.5 17547.5 17577.0 17606.5
#>  [64] 17637.0 17667.5 17698.0 17728.5 17759.5 17790.0 17820.5 17851.0 17881.5
#>  [73] 17912.5 17942.0 17971.5 18002.0 18032.5 18063.0 18093.5 18124.5 18155.0
#>  [82] 18185.5 18216.0 18246.5 18277.5 18307.5 18337.5 18368.0 18398.5 18429.0
#>  [91] 18459.5 18490.5 18521.0 18551.5 18582.0 18612.5 18643.5 18673.0 18702.5
#> [100] 18733.0 18763.5 18794.0 18824.5 18855.5 18886.0 18916.5 18947.0 18977.5
#> [109] 19008.5 19038.0 19067.5 19098.0 19128.5 19159.0 19189.5 19220.5 19251.0
#> [118] 19281.5 19312.0 19342.5 19373.5 19403.0 19432.5 19463.0 19493.5 19524.0
#> [127] 19554.5 19585.5 19616.0 19646.5 19677.0 19707.5 19738.5 19768.5 19798.5
#> [136] 19829.0 19859.5 19890.0 19920.5 19951.5 19982.0 20012.5 20043.0 20073.5
#> [145] 20104.5 20134.0 20163.5 20194.0 20224.5 20255.0 20285.5 20316.5 20347.0
#> [154] 20377.5 20408.0 20438.5 20469.5 20499.0 20528.5 20559.0 20589.5 20620.0
#> [163] 20650.5 20681.5 20712.0 20742.5 20773.0 20803.5 20834.5 20864.0 20893.5
#> [172] 20924.0 20954.5 20985.0 21015.5 21046.5 21077.0 21107.5 21138.0 21168.5
#> [181] 21199.5 21229.5 21259.5 21290.0 21320.5 21351.0 21381.5 21412.5 21443.0
#> [190] 21473.5 21504.0 21534.5 21565.5 21595.0 21624.5 21655.0 21685.5 21716.0
#> [199] 21746.5 21777.5 21808.0 21838.5 21869.0 21899.5 21930.5 21960.0 21989.5
#> [208] 22020.0 22050.5 22081.0 22111.5 22142.5 22173.0 22203.5 22234.0 22264.5
#> [217] 22295.5 22325.0 22354.5 22385.0 22415.5 22446.0 22476.5 22507.5 22538.0
#> [226] 22568.5 22599.0 22629.5 22660.5 22690.5 22720.5 22751.0 22781.5 22812.0
#> [235] 22842.5 22873.5 22904.0 22934.5 22965.0 22995.5 23026.5 23056.0 23085.5
#> [244] 23116.0 23146.5 23177.0 23207.5 23238.5 23269.0 23299.5 23330.0 23360.5
#> [253] 23391.5 23421.0 23450.5 23481.0 23511.5 23542.0 23572.5 23603.5 23634.0
#> [262] 23664.5 23695.0 23725.5 23756.5 23786.0 23815.5 23846.0 23876.5 23907.0
#> [271] 23937.5 23968.5 23999.0 24029.5 24060.0 24090.5 24121.5 24151.5 24181.5
#> [280] 24212.0 24242.5 24273.0 24303.5 24334.5 24365.0 24395.5 24426.0 24456.5
#> [289] 24487.5 24517.0 24546.5 24577.0 24607.5 24638.0 24668.5 24699.5 24730.0
#> [298] 24760.5 24791.0 24821.5 24852.5 24882.0 24911.5 24942.0 24972.5 25003.0
#> [307] 25033.5 25064.5 25095.0 25125.5 25156.0 25186.5 25217.5 25247.0 25276.5
#> [316] 25307.0 25337.5 25368.0 25398.5 25429.5 25460.0 25490.5 25521.0 25551.5

tunits <- ncatt_get(ncin,"time","units")
nt <- dim(time)
nt
#> [1] 324
tunits
#> $hasatt
#> [1] TRUE
#> 
#> $value
#> [1] "days since 1950-01-01 00:00:00"

# Get oxygen
dname <- "o2b"

oxy_array <- ncvar_get(ncin,dname)
dlname <- ncatt_get(ncin,dname,"long_name")
dunits <- ncatt_get(ncin,dname,"units")
fillvalue <- ncatt_get(ncin,dname,"_FillValue")
dim(oxy_array)
#> [1] 253 166 324

# Get global attributes
title <- ncatt_get(ncin,0,"title")
institution <- ncatt_get(ncin,0,"institution")
datasource <- ncatt_get(ncin,0,"source")
references <- ncatt_get(ncin,0,"references")
history <- ncatt_get(ncin,0,"history")
Conventions <- ncatt_get(ncin,0,"Conventions")

# Convert time: split the time units string into fields
tustr <- strsplit(tunits$value, " ")
tdstr <- strsplit(unlist(tustr)[3], "-")
tmonth <- as.integer(unlist(tdstr)[2])
tday <- as.integer(unlist(tdstr)[3])
tyear <- as.integer(unlist(tdstr)[1])

# Here I deviate from the guide a little bit. Save this info:
dates <- chron(time, origin = c(tmonth, tday, tyear))

# Crop the date variable
months <- as.numeric(substr(dates, 2, 3))
years <- as.numeric(substr(dates, 8, 9))
years <- ifelse(years > 90, 1900 + years, 2000 + years)

# Replace netCDF fill values with NA's
oxy_array[oxy_array == fillvalue$value] <- NA

# Next, we need to work with the months that correspond to the quarters that we use.
# loop through each time step, and if it is a good month save it as a raster.
# First get the index of months that correspond to Q4
months
#>   [1]  1  2  3  4  5  6  7  8  9 10 11 12  1  2  3  4  5  6  7  8  9 10 11 12  1
#>  [26]  2  3  4  5  6  7  8  9 10 11 12  1  2  3  4  5  6  7  8  9 10 11 12  1  2
#>  [51]  3  4  5  6  7  8  9 10 11 12  1  2  3  4  5  6  7  8  9 10 11 12  1  2  3
#>  [76]  4  5  6  7  8  9 10 11 12  1  2  3  4  5  6  7  8  9 10 11 12  1  2  3  4
#> [101]  5  6  7  8  9 10 11 12  1  2  3  4  5  6  7  8  9 10 11 12  1  2  3  4  5
#> [126]  6  7  8  9 10 11 12  1  2  3  4  5  6  7  8  9 10 11 12  1  2  3  4  5  6
#> [151]  7  8  9 10 11 12  1  2  3  4  5  6  7  8  9 10 11 12  1  2  3  4  5  6  7
#> [176]  8  9 10 11 12  1  2  3  4  5  6  7  8  9 10 11 12  1  2  3  4  5  6  7  8
#> [201]  9 10 11 12  1  2  3  4  5  6  7  8  9 10 11 12  1  2  3  4  5  6  7  8  9
#> [226] 10 11 12  1  2  3  4  5  6  7  8  9 10 11 12  1  2  3  4  5  6  7  8  9 10
#> [251] 11 12  1  2  3  4  5  6  7  8  9 10 11 12  1  2  3  4  5  6  7  8  9 10 11
#> [276] 12  1  2  3  4  5  6  7  8  9 10 11 12  1  2  3  4  5  6  7  8  9 10 11 12
#> [301]  1  2  3  4  5  6  7  8  9 10 11 12  1  2  3  4  5  6  7  8  9 10 11 12

index_keep_q1 <- which(months < 4)
index_keep_q4 <- which(months > 9)

oxy_q1 <- oxy_array[, , index_keep_q1]
oxy_q4 <- oxy_array[, , index_keep_q4]

months_keep_q1 <- months[index_keep_q1]
months_keep_q4 <- months[index_keep_q4]

years_keep_q1 <- years[index_keep_q1]
years_keep_q4 <- years[index_keep_q4]

# Now we have an array with only Q4 data...
# We need to now calculate the average within a year.
# Get a sequence that takes every third value between 1: number of months (length)
loop_seq_q1 <- seq(1, dim(oxy_q1)[3], by = 3)
loop_seq_q4 <- seq(1, dim(oxy_q4)[3], by = 3)

# Create objects that will hold data
dlist_q1 <- list()
dlist_q4 <- list()

oxy_1 <- c()
oxy_2 <- c()
oxy_3 <- c()
oxy_ave_q1 <- c()

oxy_10 <- c()
oxy_11 <- c()
oxy_12 <- c()
oxy_ave_q4 <- c()

# Now average by quarter. The vector loop_seq_q1 is 1, 4, 7 etc. So first i is 1, 2, 3,
# which is the index we want. 

for(i in loop_seq_q1) { # We can use q1 as looping index, doesn't matter!
  
  oxy_1 <- oxy_q1[, , (i)]
  oxy_2 <- oxy_q1[, , (i + 1)]
  oxy_3 <- oxy_q1[, , (i + 2)]
  
  oxy_10 <- oxy_q4[, , (i)]
  oxy_11 <- oxy_q4[, , (i + 1)]
  oxy_12 <- oxy_q4[, , (i + 2)]
  
  oxy_ave_q1 <- (oxy_1 + oxy_2 + oxy_3) / 3
  oxy_ave_q4 <- (oxy_10 + oxy_11 + oxy_12) / 3
    
  list_pos_q1 <- ((i/3) - (1/3)) + 1 # to get index 1:n(years)
  list_pos_q4 <- ((i/3) - (1/3)) + 1 # to get index 1:n(years)
  
  dlist_q1[[list_pos_q1]] <- oxy_ave_q1
  dlist_q4[[list_pos_q4]] <- oxy_ave_q4

}

# Now name the lists with the year:
names(dlist_q1) <- unique(years_keep_q1)
names(dlist_q4) <- unique(years_keep_q4)

# Now I need to make a loop where I extract the raster value for each year...
# The cpue data is called dat so far in this script

# Filter years in the cpue data frame to only have the years I have oxygen for
# Note: I will append the most recent data in the end, event though we don't have
# environmental data for it
d_sub_oxy_q1 <- dat %>% filter(quarter == 1) %>% filter(year %in% names(dlist_q1)) %>% droplevels()
#> filter: removed 3,586 rows (40%), 5,353 rows remaining
#> filter: removed 209 rows (4%), 5,144 rows remaining
d_sub_oxy_q4 <- dat %>% filter(quarter == 4) %>% filter(year %in% names(dlist_q4)) %>% droplevels()
#> filter: removed 5,353 rows (60%), 3,586 rows remaining
#> filter: removed 133 rows (4%), 3,453 rows remaining

# Create data holding object
data_list_q1 <- list()
data_list_q4 <- list()

# ... And for the oxygen raster
raster_list_q1 <- list()
raster_list_q4 <- list()

# Create factor year for indexing the list in the loop
d_sub_oxy_q1$year_f <- as.factor(d_sub_oxy_q1$year)
d_sub_oxy_q4$year_f <- as.factor(d_sub_oxy_q4$year)

# Loop through each year and extract raster values for the cpue data points
for(i in unique(d_sub_oxy_q1$year_f)) { # We can use q1 as looping index, doesn't matter!
  
  # Set plot limits
  ymin = 54; ymax = 58; xmin = 12; xmax = 22

  # Subset a year
  oxy_slice_q1 <- dlist_q1[[i]]
  oxy_slice_q4 <- dlist_q4[[i]]
  
  # Create raster for that year (i)
  r_q1 <- raster(t(oxy_slice_q1), xmn = min(lon), xmx = max(lon), ymn = min(lat), ymx = max(lat),
                 crs = CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs+ towgs84=0,0,0"))
  r_q4 <- raster(t(oxy_slice_q4), xmn = min(lon), xmx = max(lon), ymn = min(lat), ymx = max(lat),
                 crs = CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs+ towgs84=0,0,0"))
  
  # Flip...
  r_q1 <- flip(r_q1, direction = 'y')
  r_q4 <- flip(r_q4, direction = 'y')
  
  plot(r_q1, main = i)
  plot(r_q4, main = i)
  
  # Filter the same year (i) in the cpue data and select only coordinates
  d_slice_q1 <- d_sub_oxy_q1 %>% filter(year_f == i) %>% dplyr::select(lon, lat)
  d_slice_q4 <- d_sub_oxy_q4 %>% filter(year_f == i) %>% dplyr::select(lon, lat)
  
  # Make into a SpatialPoints object
  data_sp_q1 <- SpatialPoints(d_slice_q1)
  data_sp_q4 <- SpatialPoints(d_slice_q4)
  
  # Extract raster value (oxygen)
  rasValue_q1 <- raster::extract(r_q1, data_sp_q1, method = "bilinear")
  rasValue_q4 <- raster::extract(r_q4, data_sp_q4, method = "bilinear")
  
  # Now we want to plot the results of the raster extractions by plotting the cpue
  # data points over a raster and saving it for each year.
  # Make the SpatialPoints object into a raster again (for pl)
  df_q1 <- as.data.frame(data_sp_q1)
  df_q4 <- as.data.frame(data_sp_q4)
  
  # Add in the raster value in the df holding the coordinates for the cpue data
  d_slice_q1$oxy <- rasValue_q1
  d_slice_q4$oxy <- rasValue_q4
  
  # Add in which year
  d_slice_q1$year <- i
  d_slice_q4$year <- i
  
  # Create a index for the data last where we store all years (because our loop index
  # i is not continuous, we can't use it directly)
  index_q1 <- as.numeric(as.character(d_slice_q1$year))[1] - 1992
  index_q4 <- as.numeric(as.character(d_slice_q4$year))[1] - 1992
  
  # Add each years' data in the list
  data_list_q1[[index_q1]] <- d_slice_q1
  data_list_q4[[index_q4]] <- d_slice_q4
  
  # Save to check each year is ok! First convert the raster to points for plotting
  # (so that we can use ggplot)
  map_q1 <- rasterToPoints(r_q1)
  map_q4 <- rasterToPoints(r_q4)
  
  # Make the points a dataframe for ggplot
  df_rast_q1 <- data.frame(map_q1)
  df_rast_q4 <- data.frame(map_q4)
  
  # Rename y-variable and add year
  df_rast_q1 <- df_rast_q1 %>% rename("oxy" = "layer") %>% mutate(year = i)
  df_rast_q4 <- df_rast_q4 %>% rename("oxy" = "layer") %>% mutate(year = i)
  
  # Add each years' raster data frame in the list
  raster_list_q1[[index_q1]] <- df_rast_q1
  raster_list_q4[[index_q4]] <- df_rast_q4
  
  # Make appropriate column headings
  colnames(df_rast_q1) <- c("Longitude", "Latitude", "oxy")
  colnames(df_rast_q4) <- c("Longitude", "Latitude", "oxy")
  
  # Make a map for q1
  ggplot(data = df_rast_q1, aes(y = Latitude, x = Longitude)) +
    geom_raster(aes(fill = oxy)) +
    geom_point(data = d_slice_q1, aes(x = lon, y = lat, fill = oxy),
               color = "black", size = 5, shape = 21) +
    theme_bw() +
    geom_sf(data = world, inherit.aes = F, size = 0.2) +
    coord_sf(xlim = c(xmin, xmax),
             ylim = c(ymin, ymax)) +
    scale_colour_gradientn(colours = rev(terrain.colors(10)),
                           limits = c(-200, 400)) +
    scale_fill_gradientn(colours = rev(terrain.colors(10)),
                         limits = c(-200, 400))
  
  ggsave(paste("figures/supp/density/density_oxygen_rasters/", i,"q1.png", sep = ""),
         width = 6.5, height = 6.5, dpi = 600)
  
  # Make a map for q4
  ggplot(data = df_rast_q4, aes(y = Latitude, x = Longitude)) +
    geom_raster(aes(fill = oxy)) +
    geom_point(data = d_slice_q4, aes(x = lon, y = lat, fill = oxy),
               color = "black", size = 5, shape = 21) +
    theme_bw() +
    geom_sf(data = world, inherit.aes = F, size = 0.2) +
    coord_sf(xlim = c(xmin, xmax),
             ylim = c(ymin, ymax)) +
    scale_colour_gradientn(colours = rev(terrain.colors(10)),
                           limits = c(-200, 400)) +
    scale_fill_gradientn(colours = rev(terrain.colors(10)),
                         limits = c(-200, 400))
  
  ggsave(paste("figures/supp/density/density_oxygen_rasters/", i,"q4.png", sep = ""),
         width = 6.5, height = 6.5, dpi = 600)
  
}

#> filter: removed 5,043 rows (98%), 101 rows remaining
#> filter: removed 3,392 rows (98%), 61 rows remaining
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA

#> filter: removed 5,001 rows (97%), 143 rows remaining
#> filter: removed 3,391 rows (98%), 62 rows remaining
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA

#> filter: removed 4,990 rows (97%), 154 rows remaining
#> filter: removed 3,400 rows (98%), 53 rows remaining
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA

#> filter: removed 5,009 rows (97%), 135 rows remaining
#> filter: removed 3,392 rows (98%), 61 rows remaining
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA

#> filter: removed 5,000 rows (97%), 144 rows remaining
#> filter: removed 3,378 rows (98%), 75 rows remaining
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA

#> filter: removed 4,969 rows (97%), 175 rows remaining
#> filter: removed 3,385 rows (98%), 68 rows remaining
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA

#> filter: removed 4,973 rows (97%), 171 rows remaining
#> filter: removed 3,359 rows (97%), 94 rows remaining
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA

#> filter: removed 5,034 rows (98%), 110 rows remaining
#> filter: removed 3,364 rows (97%), 89 rows remaining
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA

#> filter: removed 4,937 rows (96%), 207 rows remaining
#> filter: removed 3,338 rows (97%), 115 rows remaining
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA

#> filter: removed 5,002 rows (97%), 142 rows remaining
#> filter: removed 3,336 rows (97%), 117 rows remaining
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA

#> filter: removed 5,008 rows (97%), 136 rows remaining
#> filter: removed 3,350 rows (97%), 103 rows remaining
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA

#> filter: removed 4,968 rows (97%), 176 rows remaining
#> filter: removed 3,368 rows (98%), 85 rows remaining
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA

#> filter: removed 4,959 rows (96%), 185 rows remaining
#> filter: removed 3,332 rows (96%), 121 rows remaining
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA

#> filter: removed 4,956 rows (96%), 188 rows remaining
#> filter: removed 3,303 rows (96%), 150 rows remaining
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA

#> filter: removed 4,909 rows (95%), 235 rows remaining
#> filter: removed 3,291 rows (95%), 162 rows remaining
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA

#> filter: removed 4,923 rows (96%), 221 rows remaining
#> filter: removed 3,277 rows (95%), 176 rows remaining
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA

#> filter: removed 4,894 rows (95%), 250 rows remaining
#> filter: removed 3,297 rows (95%), 156 rows remaining
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA

#> filter: removed 4,904 rows (95%), 240 rows remaining
#> filter: removed 3,291 rows (95%), 162 rows remaining
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA

#> filter: removed 4,901 rows (95%), 243 rows remaining
#> filter: removed 3,273 rows (95%), 180 rows remaining
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA

#> filter: removed 4,920 rows (96%), 224 rows remaining
#> filter: removed 3,320 rows (96%), 133 rows remaining
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA

#> filter: removed 4,876 rows (95%), 268 rows remaining
#> filter: removed 3,305 rows (96%), 148 rows remaining
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA

#> filter: removed 4,936 rows (96%), 208 rows remaining
#> filter: removed 3,277 rows (95%), 176 rows remaining
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA

#> filter: removed 4,907 rows (95%), 237 rows remaining
#> filter: removed 3,287 rows (95%), 166 rows remaining
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA

#> filter: removed 4,912 rows (95%), 232 rows remaining
#> filter: removed 3,240 rows (94%), 213 rows remaining
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA

#> filter: removed 4,899 rows (95%), 245 rows remaining
#> filter: removed 3,228 rows (93%), 225 rows remaining
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA

#> filter: removed 4,888 rows (95%), 256 rows remaining
#> filter: removed 3,243 rows (94%), 210 rows remaining
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA

#> filter: removed 5,026 rows (98%), 118 rows remaining
#> filter: removed 3,361 rows (97%), 92 rows remaining
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> rename: renamed one variable (oxy)
#> mutate: new variable 'year' (character) with one unique value and 0% NA


# Now create a data frame from the list of all annual values
big_dat_oxy_q1 <- dplyr::bind_rows(data_list_q1)
big_dat_oxy_q4 <- dplyr::bind_rows(data_list_q4)
big_dat_oxy <- bind_rows(mutate(big_dat_oxy_q1, quarter = 1),
                         mutate(big_dat_oxy_q4, quarter = 4))
#> mutate: new variable 'quarter' (double) with one unique value and 0% NA
#> mutate: new variable 'quarter' (double) with one unique value and 0% NA

big_raster_dat_oxy_q1 <- dplyr::bind_rows(raster_list_q1)
big_raster_dat_oxy_q4 <- dplyr::bind_rows(raster_list_q4)
big_raster_dat_oxy <- bind_rows(mutate(big_raster_dat_oxy_q1, quarter = 1),
                                mutate(big_raster_dat_oxy_q4, quarter = 4))
#> mutate: new variable 'quarter' (double) with one unique value and 0% NA
#> mutate: new variable 'quarter' (double) with one unique value and 0% NA

# Plot data, looks like there's big inter-annual variation but a negative trend over time
big_raster_dat_oxy %>%
  group_by(quarter, year) %>%
  drop_na(oxy) %>%
  summarise(mean_oxy = mean(oxy)) %>%
  mutate(year_num = as.numeric(year)) %>%
  ggplot(., aes(year_num, mean_oxy)) +
  geom_point(size = 2) +
  stat_smooth(method = "lm") +
  facet_wrap(~ quarter)
#> group_by: 2 grouping variables (quarter, year)
#> drop_na (grouped): no rows removed
#> summarise: now 54 rows and 3 columns, one group variable remaining (quarter)
#> mutate (grouped): new variable 'year_num' (double) with 27 unique values and 0% NA
#> `geom_smooth()` using formula 'y ~ x'


big_raster_dat_oxy %>%
  group_by(quarter, year) %>%
  drop_na(oxy) %>%
  mutate(dead = ifelse(oxy < 0, "Y", "N")) %>%
  filter(dead == "Y") %>%
  mutate(n = n(),
         year_num = as.numeric(year)) %>%
  ggplot(., aes(year_num, n)) +
  geom_point(size = 2) +
  stat_smooth(method = "lm") +
  facet_wrap(~ quarter)
#> group_by: 2 grouping variables (quarter, year)
#> drop_na (grouped): no rows removed
#> mutate (grouped): new variable 'dead' (character) with 2 unique values and 0% NA
#> filter (grouped): removed 877,004 rows (94%), 60,814 rows remaining
#> mutate (grouped): new variable 'n' (integer) with 54 unique values and 0% NA
#>                   new variable 'year_num' (double) with 27 unique values and 0% NA
#> `geom_smooth()` using formula 'y ~ x'


# Now add in the new oxygen column in the original data:
str(d_sub_oxy_q1)
#> tibble [5,144 × 12] (S3: tbl_df/tbl/data.frame)
#>  $ density    : num [1:5144] 158.15 10.33 5.26 19.55 812.16 ...
#>  $ year       : int [1:5144] 1993 1993 1993 1993 1993 1993 1993 1993 1993 1993 ...
#>  $ lat        : num [1:5144] 54.7 54.5 54.5 54.5 54.7 ...
#>  $ lon        : num [1:5144] 13 14.3 14.2 14 13.1 ...
#>  $ quarter    : int [1:5144] 1 1 1 1 1 1 1 1 1 1 ...
#>  $ haul.id    : chr [1:5144] "1993:1:GFR:SOL:H20:21:1" "1993:1:GFR:SOL:H20:22:32" "1993:1:GFR:SOL:H20:23:31" "1993:1:GFR:SOL:H20:24:30" ...
#>  $ IDx        : chr [1:5144] "1993.1.GFR.06S1.H20.21.1" "1993.1.GFR.06S1.H20.22.32" "1993.1.GFR.06S1.H20.23.31" "1993.1.GFR.06S1.H20.24.30" ...
#>  $ ices_rect  : chr [1:5144] "38G3" "38G4" "38G4" "37G3" ...
#>  $ sub_div    : chr [1:5144] "24" "24" "24" "24" ...
#>  $ density_fle: num [1:5144] 19.26 13.7 1.99 3.89 34.26 ...
#>  $ depth      : num [1:5144] 8.87 6.87 7.63 5.62 12.87 ...
#>  $ year_f     : Factor w/ 27 levels "1993","1994",..: 1 1 1 1 1 1 1 1 1 1 ...
str(d_sub_oxy_q4)
#> tibble [3,453 × 12] (S3: tbl_df/tbl/data.frame)
#>  $ density    : num [1:3453] 71.71 146.95 864.13 19.48 4.27 ...
#>  $ year       : int [1:3453] 1993 1993 1993 1993 1993 1993 1993 1993 1993 1993 ...
#>  $ lat        : num [1:3453] 57.5 57.5 54.7 57.1 57.8 ...
#>  $ lon        : num [1:3453] 17.1 17.6 13.7 18.9 19.5 ...
#>  $ quarter    : int [1:3453] 4 4 4 4 4 4 4 4 4 4 ...
#>  $ haul.id    : chr [1:3453] "1993:4:SWE:ARG:FOT:262:31" "1993:4:SWE:ARG:FOT:263:32" "1993:4:GFR:SOL:H20:34:60" "1993:4:SWE:ARG:FOT:256:25" ...
#>  $ IDx        : chr [1:3453] "1993.4.SWE.77AR.FOT.262.31" "1993.4.SWE.77AR.FOT.263.32" "1993.4.GFR.06S1.H20.34.60" "1993.4.SWE.77AR.FOT.256.25" ...
#>  $ ices_rect  : chr [1:3453] "43G7" "43G7" "38G3" "43G8" ...
#>  $ sub_div    : chr [1:3453] "27" "27" "24" "28" ...
#>  $ density_fle: num [1:3453] 175.9 13 230.5 25.8 46.5 ...
#>  $ depth      : num [1:3453] 71.4 86.3 19.9 76.2 86.1 ...
#>  $ year_f     : Factor w/ 27 levels "1993","1994",..: 1 1 1 1 1 1 1 1 1 1 ...
str(big_dat_oxy)
#> tibble [8,597 × 5] (S3: tbl_df/tbl/data.frame)
#>  $ lon    : num [1:8597] 13 14.3 14.2 14 13.1 ...
#>  $ lat    : num [1:8597] 54.7 54.5 54.5 54.5 54.7 ...
#>  $ oxy    : num [1:8597] 380 398 398 398 377 ...
#>  $ year   : chr [1:8597] "1993" "1993" "1993" "1993" ...
#>  $ quarter: num [1:8597] 1 1 1 1 1 1 1 1 1 1 ...

# Create an ID for matching the oxygen data with the cpue data
dat$id_oxy <- paste(dat$year, dat$quarter, dat$lon, dat$lat, sep = "_")
big_dat_oxy$id_oxy <- paste(big_dat_oxy$year, big_dat_oxy$quarter, big_dat_oxy$lon, big_dat_oxy$lat, sep = "_")

# Which id's are NOT in the cpue data (dat)?
head(dat$id_oxy, 100)
#>   [1] "1992_1_13.0333_54.95"   "1992_1_13.5_55.0333"    "1992_1_13.9_55.1"      
#>   [4] "1992_1_13.7333_54.8"    "1992_1_13.4167_54.8833" "1992_1_14.8667_54.8833"
#>   [7] "1992_1_14.5333_54.6333" "1992_1_15.1_55.6333"    "1992_1_15.0833_55.25"  
#>  [10] "1992_1_15.3_55.1"       "1992_1_15.7333_55.4333" "1992_1_15.8833_55.5833"
#>  [13] "1992_1_15.5833_55.55"   "1992_1_16.0667_55.6833" "1992_1_16.25_55.7833"  
#>  [16] "1992_1_16.4167_55.85"   "1992_1_16.9833_56.1"    "1992_1_17.65_56.1167"  
#>  [19] "1992_1_16.65_55.6333"   "1992_1_16.5833_55.5833" "1992_1_16.3333_55.3167"
#>  [22] "1992_1_16.2167_55.3"    "1992_1_16.05_55.25"     "1992_1_15.25_54.8167"  
#>  [25] "1992_1_15.0333_54.8667" "1992_1_18.3_56.1333"    "1992_1_18.3667_56.1833"
#>  [28] "1992_1_18.5333_56.15"   "1992_1_18.5333_56.2"    "1992_1_20.9833_57.2667"
#>  [31] "1992_1_20.5833_56.6"    "1992_1_20.5833_56.6"    "1992_1_20.6167_56.6167"
#>  [34] "1992_1_13.0833_54.6667" "1992_1_13.1333_54.7167" "1992_1_14.1667_54.5833"
#>  [37] "1992_1_14.25_54.6333"   "1992_1_13.3167_54.8"    "1992_1_13.4667_54.75"  
#>  [40] "1992_1_13.85_54.5667"   "1992_1_13.8667_54.75"   "1992_1_13.6667_54.7333"
#>  [43] "1992_1_14.0333_54.8667" "1992_1_13.95_54.9333"   "1992_1_13.1333_54.8833"
#>  [46] "1992_1_13.1167_54.9333" "1992_1_13.2667_54.9833" "1992_1_13.4333_55.0167"
#>  [49] "1992_1_13.75_55.1"      "1992_1_14.1667_55.15"   "1992_1_14.2667_55.1833"
#>  [52] "1992_1_14.3333_55.1"    "1992_1_14.3_55.0167"    "1992_1_13.5667_55.0333"
#>  [55] "1992_1_13.8333_55.1"    "1992_1_16.9173_57.3635" "1992_1_17.025_57.4033" 
#>  [58] "1992_1_14.6833_55.58"   "1992_1_17.57_57.4783"   "1992_1_17.515_56.1467" 
#>  [61] "1992_1_18.625_56.4767"  "1992_1_18.8767_57.1767" "1992_1_18.8683_57.1017"
#>  [64] "1992_1_18.885_57.0683"  "1992_1_15.37_55.945"    "1992_1_18.9_57.0217"   
#>  [67] "1992_1_17.7967_56.0966" "1992_1_17.7017_55.855"  "1992_1_17.1317_55.9267"
#>  [70] "1992_1_16.5233_55.6783" "1992_1_14.4433_55.695"  "1992_1_15.9483_55.4533"
#>  [73] "1992_1_15.8866_55.7983" "1992_1_19.1617_57.3617" "1992_1_19.5333_57.8167"
#>  [76] "1992_1_19.53_57.885"    "1992_1_19.4633_58.0566" "1992_1_18.1183_57.8083"
#>  [79] "1992_1_17.9117_57.3733" "1992_1_17.915_57.1017"  "1992_1_17.965_57.045"  
#>  [82] "1992_1_17.2983_57.14"   "1992_1_16.065_55.8767"  "1992_1_14.6483_55.4517"
#>  [85] "1992_1_14.4457_55.456"  "1992_1_13.2_55.2083"    "1992_1_13.5917_55.21"  
#>  [88] "1992_1_13.25_54.97"     "1992_1_13.9233_55.0133" "1992_4_13.45_55.0167"  
#>  [91] "1992_4_13.1_54.9167"    "1992_4_13.9667_54.9333" "1992_4_14.35_55.1"     
#>  [94] "1992_4_14.1667_55.1333" "1992_4_13.9_54.5"       "1992_4_14.2_54.5"      
#>  [97] "1992_4_14.2833_55.1833" "1992_4_13.8_54.5167"    "1992_4_14.3667_54.5167"
#> [100] "1992_4_13.8833_54.5667"
head(big_dat_oxy$id_oxy, 100)
#>   [1] "1993_1_13_54.6833"      "1993_1_14.2667_54.5167" "1993_1_14.15_54.5167"  
#>   [4] "1993_1_13.9833_54.4833" "1993_1_13.0833_54.7333" "1993_1_13.1833_54.7"   
#>   [7] "1993_1_13.8_54.55"      "1993_1_14.1167_54.7"    "1993_1_14.0833_54.5833"
#>  [10] "1993_1_13.8667_54.5833" "1993_1_14.15_54.6333"   "1993_1_13.3667_54.7333"
#>  [13] "1993_1_13.5167_54.7333" "1993_1_13.6333_54.6833" "1993_1_13.7333_54.6667"
#>  [16] "1993_1_13.85_54.7"      "1993_1_13.6667_54.7667" "1993_1_14.0167_54.85"  
#>  [19] "1993_1_13.8833_54.9667" "1993_1_13.6167_54.9167" "1993_1_13.2167_54.8667"
#>  [22] "1993_1_13.1_54.9333"    "1993_1_13.3167_54.9833" "1993_1_13.4333_55.0333"
#>  [25] "1993_1_13.75_55.0667"   "1993_1_14.1667_55.1333" "1993_1_14.2667_55.1667"
#>  [28] "1993_1_14.2833_55.0667" "1993_1_14.2333_55"      "1993_1_13.6167_55.05"  
#>  [31] "1993_1_13.8167_55.0833" "1993_1_16.6_54.8667"    "1993_1_15.1167_54.3"   
#>  [34] "1993_1_16.5167_54.8333" "1993_1_15.0167_54.4333" "1993_1_16.6333_54.85"  
#>  [37] "1993_1_15.7667_54.3667" "1993_1_16.0167_54.4333" "1993_1_15.3333_55.9333"
#>  [40] "1993_1_15.7167_54.4333" "1993_1_16.2167_54.9167" "1993_1_16.3_55.8333"   
#>  [43] "1993_1_15.95_54.7833"   "1993_1_17.6167_56.0833" "1993_1_15.6_55.9167"   
#>  [46] "1993_1_15.5833_54.6167" "1993_1_16.2333_55.7833" "1993_1_17.75_56.1333"  
#>  [49] "1993_1_15.1167_55.6833" "1993_1_16.4333_55.45"   "1993_1_14.8667_55.6167"
#>  [52] "1993_1_15.6_54.7667"    "1993_1_16.35_55.3667"   "1993_1_15.8833_55.6"   
#>  [55] "1993_1_15.7_55.6"       "1993_1_17.35_55.2667"   "1993_1_16.2_55.3"      
#>  [58] "1993_1_15.1667_55.4333" "1993_1_17.55_55.25"     "1993_1_16.05_55.1333"  
#>  [61] "1993_1_14.5143_55.6712" "1993_1_16.611_55.6187"  "1993_1_16.8935_55.785" 
#>  [64] "1993_1_14.7333_55.455"  "1993_1_14.5483_55.465"  "1993_1_13.9913_54.9983"
#>  [67] "1993_1_13.6493_55.213"  "1993_1_13.32_54.935"    "1993_1_13.2767_55.21"  
#>  [70] "1993_1_15.4393_55.8218" "1993_1_15.46_55.9933"   "1993_1_19.5368_57.8885"
#>  [73] "1993_1_16.1217_55.795"  "1993_1_16.085_55.8733"  "1993_1_17.0855_55.8667"
#>  [76] "1993_1_17.1323_55.9248" "1993_1_17.7367_55.8015" "1993_1_17.7188_56.0352"
#>  [79] "1993_1_16.9745_56.445"  "1993_1_16.8225_56.5178" "1993_1_17.94_57.004"   
#>  [82] "1993_1_14.7172_55.5687" "1993_1_17.9145_57.066"  "1993_1_16.9133_57.3685"
#>  [85] "1993_1_17.0157_57.5172" "1993_1_17.5322_57.4502" "1993_1_18.123_57.821"  
#>  [88] "1993_1_18.3128_57.7555" "1993_1_16.0507_55.3282" "1993_1_15.3205_55.412" 
#>  [91] "1993_1_19.5493_57.8677" "1993_1_19.0568_57.5685" "1993_1_19.1942_57.3825"
#>  [94] "1993_1_18.8761_57.1808" "1993_1_18.8363_57.085"  "1993_1_18.9005_57.089" 
#>  [97] "1993_1_18.4828_56.2562" "1993_1_18.3732_55.5878" "1993_1_16.9951_55.2882"
#> [100] "1993_1_17.5_55"
tail(dat$id_oxy, 100)
#>   [1] "2018_4_20.37_57.0317"   "2018_4_20.3383_56.6"    "2018_4_20.1317_56.4817"
#>   [4] "2018_4_20.0833_56.4333" "2018_4_19.7067_56.38"   "2018_4_19.525_56.3"    
#>   [7] "2018_4_19.4933_58.035"  "2018_4_19.5305_57.8297" "2018_4_18.1183_57.79"  
#>  [10] "2018_4_16.0635_55.1584" "2018_4_16.053_55.1374"  "2018_4_15.7322_55.0559"
#>  [13] "2018_4_17.7017_55.78"   "2018_4_20.5933_57.34"   "2018_4_15.4397_55.7654"
#>  [16] "2018_4_14.7118_55.6254" "2018_4_14.8696_55.623"  "2018_4_15.255_55.5437" 
#>  [19] "2018_4_16.1757_55.5421" "2018_4_15.5875_55.5354" "2018_4_14.9434_55.5179"
#>  [22] "2018_4_15.5546_55.466"  "2018_4_15.9157_55.4343" "2018_4_15.2662_55.3745"
#>  [25] "2018_4_16.0234_55.3034" "2018_4_15.9613_55.2382" "2018_4_19.4517_56.2"   
#>  [28] "2018_4_20.2267_55.7667" "2018_4_20.06_55.735"    "2018_4_19.9767_55.6683"
#>  [31] "2018_4_20.275_55.6583"  "2018_4_20.1567_55.6433" "2018_4_18.9933_55.5083"
#>  [34] "2019_1_18.5467_56.3017" "2019_1_19.4592_56.199"  "2019_1_19.5188_56.28"  
#>  [37] "2019_1_19.6268_56.3762" "2019_1_19.8065_56.4432" "2019_1_17.9563_57.0228"
#>  [40] "2019_1_17.9102_57.074"  "2019_1_17.2842_57.0972" "2019_1_17.4182_57.3255"
#>  [43] "2019_1_19.1467_57.3458" "2019_1_18.3733_55.605"  "2019_1_19.083_57.3555" 
#>  [46] "2019_1_17.5442_57.4563" "2019_1_19.2515_57.4993" "2019_1_18.5867_56.385" 
#>  [49] "2019_1_17.0822_57.6312" "2019_1_19.4818_57.7923" "2019_1_15.73_54.3783"  
#>  [52] "2019_4_19.1013_57.2805" "2019_4_17.8862_57.3405" "2019_4_16.997_57.4457" 
#>  [55] "2019_4_17.0787_57.6348" "2019_4_18.3157_57.755"  "2019_4_18.1202_57.7905"
#>  [58] "2019_4_19.5262_57.8475" "2019_4_19.5033_56.2167" "2019_4_19.4908_56.2172"
#>  [61] "2019_4_18.6763_56.3202" "2019_4_18.6765_56.4617" "2020_1_18.8967_54.55"  
#>  [64] "2020_1_18.7983_55.3583" "2020_1_17.0433_55.3833" "2020_1_19.049_57.1968" 
#>  [67] "2020_1_18.828_57.1662"  "2020_1_18.9622_57.1165" "2020_1_19.439_58.075"  
#>  [70] "2020_1_19.4817_57.7677" "2020_1_17.7363_57.4597" "2020_1_17.9093_57.0747"
#>  [73] "2020_1_18.8312_57.0655" "2020_1_18.8742_57.0632" "2020_1_19.11_57.321"   
#>  [76] "2020_1_19.2567_54.3883" "2020_4_21.4283_57.4983" "2020_4_20.5433_57.0467"
#>  [79] "2020_4_20.08_56.3833"   "2020_4_18.015_55.69"    "2020_4_16.9165_57.3647"
#>  [82] "2020_4_19.2267_57.3361" "2020_4_19.1085_57.319"  "2020_4_17.4146_57.2805"
#>  [85] "2020_4_19.0668_57.2496" "2020_4_18.9076_57.089"  "2020_4_19.4891_58.0354"
#>  [88] "2020_4_19.5044_57.876"  "2020_4_18.1196_57.7804" "2020_4_19.4807_57.7685"
#>  [91] "2020_4_19.3769_57.6885" "2020_4_17.0977_57.6175" "2020_4_17.5616_57.4828"
#>  [94] "2020_4_17.0912_57.4622" "2020_4_17.9167_57.0532" "2020_4_17.9509_56.9956"
#>  [97] "2020_4_17.184_56.9584"  "2020_4_16.9939_56.7256" "2020_4_16.85_56.5655"  
#> [100] "2020_4_19.439_58.0746"
tail(big_dat_oxy$id_oxy, 100)
#>   [1] "2018_4_15.9613_55.2382" "2018_4_19.4517_56.2"    "2018_4_20.2267_55.7667"
#>   [4] "2018_4_20.06_55.735"    "2018_4_19.9767_55.6683" "2018_4_20.275_55.6583" 
#>   [7] "2018_4_20.1567_55.6433" "2018_4_18.9933_55.5083" "2019_4_17.3133_55.3233"
#>  [10] "2019_4_14.529_55.4627"  "2019_4_18.6817_54.785"  "2019_4_18.7083_54.7833"
#>  [13] "2019_4_18.75_54.7317"   "2019_4_18.9583_54.565"  "2019_4_14.6418_55.4718"
#>  [16] "2019_4_18.535_54.8133"  "2019_4_17.2767_55.3117" "2019_4_14.3668_55.696" 
#>  [19] "2019_4_15.955_54.75"    "2019_4_16.915_57.365"   "2019_4_14.3798_55.7077"
#>  [22] "2019_4_17.3167_55.2383" "2019_4_14.4607_55.6847" "2019_4_14.51_55.6878"  
#>  [25] "2019_4_14.374_55.7028"  "2019_4_17.9512_56.992"  "2019_4_19.1683_54.6"   
#>  [28] "2019_4_17.9208_57.0495" "2019_4_19.5073_57.8783" "2019_4_19.31_54.6267"  
#>  [31] "2019_4_18.8628_57.1762" "2019_4_17.1777_56.944"  "2019_4_19.2217_54.6333"
#>  [34] "2019_4_19.3283_54.45"   "2019_4_19.3033_54.4283" "2019_4_19.305_54.4167" 
#>  [37] "2019_4_19.2983_54.4"    "2019_4_16.885_54.7417"  "2019_4_16.8333_54.7483"
#>  [40] "2019_4_16.6767_54.7017" "2019_4_16.4183_54.6567" "2019_4_16.045_54.83"   
#>  [43] "2019_4_16.0683_54.6317" "2019_4_15.8267_54.4667" "2019_4_18.961_57.2812" 
#>  [46] "2019_4_16.035_54.4083"  "2019_4_16.0567_54.4383" "2019_4_15.7667_54.38"  
#>  [49] "2019_4_15.6433_54.3817" "2019_4_16.21_55.045"    "2019_4_17.4917_54.8517"
#>  [52] "2019_4_17.4233_54.955"  "2019_4_18.4888_56.3935" "2019_4_17.4533_54.995" 
#>  [55] "2019_4_16.705_54.8783"  "2019_4_16.8033_55.42"   "2019_4_17.2667_55.4567"
#>  [58] "2019_4_17.3933_55.385"  "2019_4_18.1817_54.9017" "2019_4_18.535_54.985"  
#>  [61] "2019_4_18.6372_55.9193" "2019_4_18.58_54.8517"   "2019_4_18.4312_56.2255"
#>  [64] "2019_4_18.5417_54.9817" "2019_4_18.6_54.9133"    "2019_4_18.4437_56.3492"
#>  [67] "2019_4_18.635_54.8883"  "2019_4_18.655_54.895"   "2019_4_19.1783_54.4317"
#>  [70] "2019_4_19.0633_54.4433" "2019_4_19.0617_54.4367" "2019_4_16.9977_56.6928"
#>  [73] "2019_4_19.0417_54.4017" "2019_4_19.0167_54.405"  "2019_4_15.5732_55.8533"
#>  [76] "2019_4_18.99_54.4017"   "2019_4_19.04_54.94"     "2019_4_15.2227_55.7868"
#>  [79] "2019_4_18.8167_54.9733" "2019_4_18.8_54.965"     "2019_4_18.645_55.1767" 
#>  [82] "2019_4_18.33_55.465"    "2019_4_14.4635_55.6833" "2019_4_18.325_55.4583" 
#>  [85] "2019_4_18.7733_55.8883" "2019_4_18.0533_55.6317" "2019_4_17.5967_55.6167"
#>  [88] "2019_4_17.5533_55.3933" "2019_4_17.76_55.395"    "2019_4_19.1013_57.2805"
#>  [91] "2019_4_17.8862_57.3405" "2019_4_16.997_57.4457"  "2019_4_17.0787_57.6348"
#>  [94] "2019_4_18.3157_57.755"  "2019_4_18.1202_57.7905" "2019_4_19.5262_57.8475"
#>  [97] "2019_4_19.5033_56.2167" "2019_4_19.4908_56.2172" "2019_4_18.6763_56.3202"
#> [100] "2019_4_18.6765_56.4617"
#dat %>% group_by(year) %>% summarise(n = n()) %>% as.data.frame()

ids <- dat$id_oxy[!dat$id_oxy %in% c(big_dat_oxy$id_oxy)]

dat %>% filter(id_oxy %in% ids)
#> filter: removed 8,597 rows (96%), 342 rows remaining
#> # A tibble: 342 × 12
#>    density  year   lat   lon quarter haul.id IDx   ices_…¹ sub_div densi…² depth
#>      <dbl> <int> <dbl> <dbl>   <int> <chr>   <chr> <chr>   <chr>     <dbl> <dbl>
#>  1    25.0  1992  55.0  13.0       1 1992:1… 1992… 38G3    24         13.1  22.3
#>  2    96.4  1992  55.0  13.5       1 1992:1… 1992… 39G3    24        117.   30.9
#>  3   141.   1992  55.1  13.9       1 1992:1… 1992… 39G3    24        267.   31.9
#>  4   102.   1992  54.8  13.7       1 1992:1… 1992… 38G3    24         57.2  27.9
#>  5   133.   1992  54.9  13.4       1 1992:1… 1992… 38G3    24         80.6  31.9
#>  6    46.4  1992  54.9  14.9       1 1992:1… 1992… 38G4    24         21.3  39.5
#>  7    45.4  1992  54.6  14.5       1 1992:1… 1992… 38G4    24         17.8  21.6
#>  8   733.   1992  55.6  15.1       1 1992:1… 1992… 40G5    25        790.   71.4
#>  9   133.   1992  55.2  15.1       1 1992:1… 1992… 39G5    25        141.   61.9
#> 10   200.   1992  55.1  15.3       1 1992:1… 1992… 39G5    25        286.   69.9
#> # … with 332 more rows, 1 more variable: id_oxy <chr>, and abbreviated variable
#> #   names ¹​ices_rect, ²​density_fle

# Select only the columns we want to merge
big_dat_sub_oxy <- big_dat_oxy %>% dplyr::select(id_oxy, oxy)

# Remove duplicate ID (one oxy value per id)
big_dat_sub_oxy %>% group_by(id_oxy) %>% mutate(n = n()) %>% arrange(desc(n))
#> group_by: one grouping variable (id_oxy)
#> mutate (grouped): new variable 'n' (integer) with 4 unique values and 0% NA
#> # A tibble: 8,597 × 3
#> # Groups:   id_oxy [8,576]
#>    id_oxy                   oxy     n
#>    <chr>                  <dbl> <int>
#>  1 1999_1_15.3667_54.5667  298.     4
#>  2 1999_1_15.3667_54.5667  298.     4
#>  3 1999_1_15.3667_54.5667  298.     4
#>  4 1999_1_15.3667_54.5667  298.     4
#>  5 1999_4_15.2833_55.8     184.     3
#>  6 1999_4_15.2833_55.8     184.     3
#>  7 1999_4_15.2833_55.8     184.     3
#>  8 1995_1_18.7833_54.6833  385.     2
#>  9 1995_1_18.7833_54.6833  385.     2
#> 10 1995_1_18.65_54.8667    384.     2
#> # … with 8,587 more rows
big_dat_sub_oxy2 <- big_dat_sub_oxy %>% distinct(id_oxy, .keep_all = TRUE)
#> distinct: removed 21 rows (<1%), 8,576 rows remaining

# Join the data with raster-derived oxygen with the full cpue data
nrow(dat)
#> [1] 8939
dat <- left_join(dat, big_dat_sub_oxy2, by = "id_oxy")
#> left_join: added one column (oxy)
#>            > rows only in x     342
#>            > rows only in y  (    0)
#>            > matched rows     8,597
#>            >                 =======
#>            > rows total       8,939
nrow(dat)
#> [1] 8939

# Now the unit of oxygen is mmol/m3. I want it to be ml/L. The original model is in unit ml/L
# and it's been converted by the data host. Since it was converted without accounting for
# pressure or temperature, I can simply use the following conversion factor:
# 1 ml/l = 103/22.391 = 44.661 μmol/l -> 1 ml/l = 0.044661 mmol/l = 44.661 mmol/m^3 -> 0.0223909 ml/l = 1mmol/m^3
# https://ocean.ices.dk/tools/unitconversion.aspx

dat$oxy <- dat$oxy * 0.0223909

# Drop NA oxygen
dat <- dat %>% drop_na(oxy)
#> drop_na: removed 343 rows (4%), 8,596 rows remaining

sort(unique(dat$year))
#>  [1] 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007
#> [16] 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019

Temperature

# Open the netCDF file
ncin <- nc_open("data/NEMO_Nordic_SCOBI/dataset-reanalysis-nemo-monthlymeans_1608127623694.nc")

print(ncin)
#> File data/NEMO_Nordic_SCOBI/dataset-reanalysis-nemo-monthlymeans_1608127623694.nc (NC_FORMAT_CLASSIC):
#> 
#>      1 variables (excluding dimension variables):
#>         float bottomT[longitude,latitude,time]   
#>             standard_name: sea_water_potential_temperature_at_sea_floor
#>             units: degrees_C
#>             long_name: Sea floor potential temperature
#>             missing_value: NaN
#>             _FillValue: NaN
#>             _ChunkSizes: 1
#>              _ChunkSizes: 523
#>              _ChunkSizes: 383
#> 
#>      3 dimensions:
#>         time  Size:324
#>             axis: T
#>             long_name: Validity time
#>             standard_name: time
#>             units: days since 1950-01-01 00:00:00
#>             calendar: gregorian
#>             _ChunkSizes: 512
#>             _CoordinateAxisType: Time
#>             valid_min: 15721.5
#>             valid_max: 25551.5
#>         latitude  Size:523
#>             axis: Y
#>             standard_name: latitude
#>             long_name: latitude
#>             units: degrees_north
#>             _CoordinateAxisType: Lat
#>             valid_min: 48.49169921875
#>             valid_max: 65.8914184570312
#>         longitude  Size:383
#>             standard_name: longitude
#>             long_name: longitude
#>             units: degrees_east
#>             axis: X
#>             _CoordinateAxisType: Lon
#>             valid_min: 9.01375484466553
#>             valid_max: 30.2357654571533
#> 
#>     24 global attributes:
#>         references: http://www.smhi.se
#>         institution: Swedish Meterological and Hydrological Institute
#>         history: See source and creation_date attributees
#>         Conventions: CF-1.5
#>         contact: servicedesk_cmems@mercator-ocean.eu
#>         comment: Provided by SMHI as a Copernicus Marine Environment Monitoring Service production unit
#>         bullentin_type: reanalysis
#>         cmems_product_id: BALTICSEA_REANALYSIS_PHY_003_011
#>         title: CMEMS V4 Reanalysis: NEMO model 3D fields (monthly means)
#>         FROM_ORIGINAL_FILE__easternmost_longitude: 30.2357654571533
#>         FROM_ORIGINAL_FILE__northernmost_latitude: 65.8914184570312
#>         FROM_ORIGINAL_FILE__westernmost_longitude: 9.01375484466553
#>         FROM_ORIGINAL_FILE__southernmost_latitude: 48.49169921875
#>         shallowest_depth: 1.50136542320251
#>         deepest_depth: 711.059204101562
#>         source: SMHI reanalysis run NORDIC-NS2_1d_20191201_20191202
#>         file_quality_index: 1
#>         creation_date: 2020-11-16 UTC
#>         bullentin_date: 20191201
#>         start_date: 2019-12-01 UTC
#>         stop_date: 2019-12-01 UTC
#>         start_time: 00:00 UTC
#>         stop_time: 00:00 UTC
#>         _CoordSysBuilder: ucar.nc2.dataset.conv.CF1Convention

# Get longitude and latitude
lon <- ncvar_get(ncin,"longitude")
nlon <- dim(lon)
head(lon)
#> [1] 9.013755 9.069310 9.124865 9.180420 9.235975 9.291530

lat <- ncvar_get(ncin,"latitude")
nlat <- dim(lat)
head(lat)
#> [1] 48.49170 48.52503 48.55836 48.59170 48.62503 48.65836

# Get time
time <- ncvar_get(ncin,"time")
time
#>   [1] 15721.5 15751.0 15780.5 15811.0 15841.5 15872.0 15902.5 15933.5 15964.0
#>  [10] 15994.5 16025.0 16055.5 16086.5 16116.0 16145.5 16176.0 16206.5 16237.0
#>  [19] 16267.5 16298.5 16329.0 16359.5 16390.0 16420.5 16451.5 16481.0 16510.5
#>  [28] 16541.0 16571.5 16602.0 16632.5 16663.5 16694.0 16724.5 16755.0 16785.5
#>  [37] 16816.5 16846.5 16876.5 16907.0 16937.5 16968.0 16998.5 17029.5 17060.0
#>  [46] 17090.5 17121.0 17151.5 17182.5 17212.0 17241.5 17272.0 17302.5 17333.0
#>  [55] 17363.5 17394.5 17425.0 17455.5 17486.0 17516.5 17547.5 17577.0 17606.5
#>  [64] 17637.0 17667.5 17698.0 17728.5 17759.5 17790.0 17820.5 17851.0 17881.5
#>  [73] 17912.5 17942.0 17971.5 18002.0 18032.5 18063.0 18093.5 18124.5 18155.0
#>  [82] 18185.5 18216.0 18246.5 18277.5 18307.5 18337.5 18368.0 18398.5 18429.0
#>  [91] 18459.5 18490.5 18521.0 18551.5 18582.0 18612.5 18643.5 18673.0 18702.5
#> [100] 18733.0 18763.5 18794.0 18824.5 18855.5 18886.0 18916.5 18947.0 18977.5
#> [109] 19008.5 19038.0 19067.5 19098.0 19128.5 19159.0 19189.5 19220.5 19251.0
#> [118] 19281.5 19312.0 19342.5 19373.5 19403.0 19432.5 19463.0 19493.5 19524.0
#> [127] 19554.5 19585.5 19616.0 19646.5 19677.0 19707.5 19738.5 19768.5 19798.5
#> [136] 19829.0 19859.5 19890.0 19920.5 19951.5 19982.0 20012.5 20043.0 20073.5
#> [145] 20104.5 20134.0 20163.5 20194.0 20224.5 20255.0 20285.5 20316.5 20347.0
#> [154] 20377.5 20408.0 20438.5 20469.5 20499.0 20528.5 20559.0 20589.5 20620.0
#> [163] 20650.5 20681.5 20712.0 20742.5 20773.0 20803.5 20834.5 20864.0 20893.5
#> [172] 20924.0 20954.5 20985.0 21015.5 21046.5 21077.0 21107.5 21138.0 21168.5
#> [181] 21199.5 21229.5 21259.5 21290.0 21320.5 21351.0 21381.5 21412.5 21443.0
#> [190] 21473.5 21504.0 21534.5 21565.5 21595.0 21624.5 21655.0 21685.5 21716.0
#> [199] 21746.5 21777.5 21808.0 21838.5 21869.0 21899.5 21930.5 21960.0 21989.5
#> [208] 22020.0 22050.5 22081.0 22111.5 22142.5 22173.0 22203.5 22234.0 22264.5
#> [217] 22295.5 22325.0 22354.5 22385.0 22415.5 22446.0 22476.5 22507.5 22538.0
#> [226] 22568.5 22599.0 22629.5 22660.5 22690.5 22720.5 22751.0 22781.5 22812.0
#> [235] 22842.5 22873.5 22904.0 22934.5 22965.0 22995.5 23026.5 23056.0 23085.5
#> [244] 23116.0 23146.5 23177.0 23207.5 23238.5 23269.0 23299.5 23330.0 23360.5
#> [253] 23391.5 23421.0 23450.5 23481.0 23511.5 23542.0 23572.5 23603.5 23634.0
#> [262] 23664.5 23695.0 23725.5 23756.5 23786.0 23815.5 23846.0 23876.5 23907.0
#> [271] 23937.5 23968.5 23999.0 24029.5 24060.0 24090.5 24121.5 24151.5 24181.5
#> [280] 24212.0 24242.5 24273.0 24303.5 24334.5 24365.0 24395.5 24426.0 24456.5
#> [289] 24487.5 24517.0 24546.5 24577.0 24607.5 24638.0 24668.5 24699.5 24730.0
#> [298] 24760.5 24791.0 24821.5 24852.5 24882.0 24911.5 24942.0 24972.5 25003.0
#> [307] 25033.5 25064.5 25095.0 25125.5 25156.0 25186.5 25217.5 25247.0 25276.5
#> [316] 25307.0 25337.5 25368.0 25398.5 25429.5 25460.0 25490.5 25521.0 25551.5

tunits <- ncatt_get(ncin,"time","units")
nt <- dim(time)
nt
#> [1] 324
tunits
#> $hasatt
#> [1] TRUE
#> 
#> $value
#> [1] "days since 1950-01-01 00:00:00"

# Get temperature
dname <- "bottomT"

temp_array <- ncvar_get(ncin,dname)
dlname <- ncatt_get(ncin,dname,"long_name")
dunits <- ncatt_get(ncin,dname,"units")
fillvalue <- ncatt_get(ncin,dname,"_FillValue")
dim(temp_array)
#> [1] 383 523 324

# Get global attributes
title <- ncatt_get(ncin,0,"title")
institution <- ncatt_get(ncin,0,"institution")
datasource <- ncatt_get(ncin,0,"source")
references <- ncatt_get(ncin,0,"references")
history <- ncatt_get(ncin,0,"history")
Conventions <- ncatt_get(ncin,0,"Conventions")

# Convert time: split the time units string into fields
tustr <- strsplit(tunits$value, " ")
tdstr <- strsplit(unlist(tustr)[3], "-")
tmonth <- as.integer(unlist(tdstr)[2])
tday <- as.integer(unlist(tdstr)[3])
tyear <- as.integer(unlist(tdstr)[1])

# Here I deviate from the guide a little bit. Save this info:
dates <- chron(time, origin = c(tmonth, tday, tyear))

# Crop the date variable
months <- as.numeric(substr(dates, 2, 3))
years <- as.numeric(substr(dates, 8, 9))
years <- ifelse(years > 90, 1900 + years, 2000 + years)

# Replace netCDF fill values with NA's
temp_array[temp_array == fillvalue$value] <- NA

# We only use Quarter 4 in this analysis, so now we want to loop through each time step,
# and if it is a good month save it as a raster.
# First get the index of months that correspond to Q4
months
#>   [1]  1  2  3  4  5  6  7  8  9 10 11 12  1  2  3  4  5  6  7  8  9 10 11 12  1
#>  [26]  2  3  4  5  6  7  8  9 10 11 12  1  2  3  4  5  6  7  8  9 10 11 12  1  2
#>  [51]  3  4  5  6  7  8  9 10 11 12  1  2  3  4  5  6  7  8  9 10 11 12  1  2  3
#>  [76]  4  5  6  7  8  9 10 11 12  1  2  3  4  5  6  7  8  9 10 11 12  1  2  3  4
#> [101]  5  6  7  8  9 10 11 12  1  2  3  4  5  6  7  8  9 10 11 12  1  2  3  4  5
#> [126]  6  7  8  9 10 11 12  1  2  3  4  5  6  7  8  9 10 11 12  1  2  3  4  5  6
#> [151]  7  8  9 10 11 12  1  2  3  4  5  6  7  8  9 10 11 12  1  2  3  4  5  6  7
#> [176]  8  9 10 11 12  1  2  3  4  5  6  7  8  9 10 11 12  1  2  3  4  5  6  7  8
#> [201]  9 10 11 12  1  2  3  4  5  6  7  8  9 10 11 12  1  2  3  4  5  6  7  8  9
#> [226] 10 11 12  1  2  3  4  5  6  7  8  9 10 11 12  1  2  3  4  5  6  7  8  9 10
#> [251] 11 12  1  2  3  4  5  6  7  8  9 10 11 12  1  2  3  4  5  6  7  8  9 10 11
#> [276] 12  1  2  3  4  5  6  7  8  9 10 11 12  1  2  3  4  5  6  7  8  9 10 11 12
#> [301]  1  2  3  4  5  6  7  8  9 10 11 12  1  2  3  4  5  6  7  8  9 10 11 12

index_keep <- which(months > 9)

# Quarter 4 by keeping months in index_keep
temp_q4 <- temp_array[, , index_keep]

months_keep <- months[index_keep]

years_keep <- years[index_keep]

# Now we have an array with only Q4 data...
# We need to now calculate the average within a year.
# Get a sequence that takes every third value between 1: number of months (length)
loop_seq <- seq(1, dim(temp_q4)[3], by = 3)

# Create objects that will hold data
dlist <- list()
temp_10 <- c()
temp_11 <- c()
temp_12 <- c()
temp_ave <- c()

# Loop through the vector sequence with every third value, then take the average of
# three consecutive months (i.e. q4)
for(i in loop_seq) {
  
  temp_10 <- temp_q4[, , (i)]
  temp_11 <- temp_q4[, , (i + 1)]
  temp_12 <- temp_q4[, , (i + 2)]
  
  temp_ave <- (temp_10 + temp_11 + temp_12) / 3
  
  list_pos <- ((i/3) - (1/3)) + 1 # to get index 1:n(years)
  
  dlist[[list_pos]] <- temp_ave
  
}

# Now name the lists with the year:
names(dlist) <- unique(years_keep)

# Now I need to make a loop where I extract the raster value for each year...
# The cpue data is called dat so far in this script

# Filter years in the cpue data frame to only have the years I have temperature for
d_sub_temp <- dat %>% filter(year %in% names(dlist)) %>% droplevels()
#> filter: no rows removed

# Create data holding object
data_list <- list()

# ... And for the temperature raster
raster_list <- list()

# Create factor year for indexing the list in the loop
d_sub_temp$year_f <- as.factor(d_sub_temp$year)

# Loop through each year and extract raster values for the cpue data points
for(i in unique(d_sub_temp$year_f)) {
  
  # Subset a year
  temp_slice <- dlist[[i]]
  
  # Create raster for that year (i)
  r <- raster(t(temp_slice), xmn = min(lon), xmx = max(lon), ymn = min(lat), ymx = max(lat),
              crs = CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs+ towgs84=0,0,0"))
  
  # Flip...
  r <- flip(r, direction = 'y')
  
  #plot(r, main = i)
  
  # Filter the same year (i) in the cpue data and select only coordinates
  d_slice <- d_sub_temp %>% filter(year_f == i) %>% dplyr::select(lon, lat)
  
  # Make into a SpatialPoints object
  data_sp <- SpatialPoints(d_slice)
  
  # Extract raster value (temperature)
  rasValue <- raster::extract(r, data_sp, method = "bilinear")
  
  # Now we want to plot the results of the raster extractions by plotting the cpue
  # data points over a raster and saving it for each year.
  # Make the SpatialPoints object into a raster again (for pl)
  df <- as.data.frame(data_sp)
  
  # Add in the raster value in the df holding the coordinates for the cpue data
  d_slice$temp <- rasValue
  
  # Add in which year
  d_slice$year <- i
  
  # Create a index for the data last where we store all years (because our loop index
  # i is not continuous, we can't use it directly)
  index <- as.numeric(d_slice$year)[1] - 1992
  
  # Add each years' data in the list
  data_list[[index]] <- d_slice
  
  # Save to check each year is ok! First convert the raster to points for plotting
  # (so that we can use ggplot)
  map.p <- rasterToPoints(r)
  
  # Make the points a dataframe for ggplot
  df_rast <- data.frame(map.p)
  
  # Rename y-variable and add year
  df_rast <- df_rast %>% rename("temp" = "layer") %>% mutate(year = i)
  
  # Add each years' raster data frame in the list
  raster_list[[index]] <- df_rast
  
  # Make appropriate column headings
  colnames(df_rast) <- c("Longitude", "Latitude", "temp")
  
  # Now make the map
  ggplot(data = df_rast, aes(y = Latitude, x = Longitude)) +
    geom_raster(aes(fill = temp)) +
    geom_point(data = d_slice, aes(x = lon, y = lat, fill = temp),
               color = "black", size = 5, shape = 21) +
    theme_bw() +
    geom_sf(data = world, inherit.aes = F, size = 0.2) +
    coord_sf(xlim = c(min(dat$lon), max(dat$lon)),
             ylim = c(min(dat$lat), max(dat$lat))) +
    scale_colour_gradientn(colours = rev(terrain.colors(10)),
                           limits = c(2, 17)) +
    scale_fill_gradientn(colours = rev(terrain.colors(10)),
                         limits = c(2, 17))
  
  ggsave(paste("figures/supp/density/density_temp_rasters/", i,".png", sep = ""),
         width = 6.5, height = 6.5, dpi = 600)
  
}
#> filter: removed 8,434 rows (98%), 162 rows remaining
#> rename: renamed one variable (temp)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> filter: removed 8,391 rows (98%), 205 rows remaining
#> rename: renamed one variable (temp)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> filter: removed 8,389 rows (98%), 207 rows remaining
#> rename: renamed one variable (temp)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> filter: removed 8,400 rows (98%), 196 rows remaining
#> rename: renamed one variable (temp)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> filter: removed 8,377 rows (97%), 219 rows remaining
#> rename: renamed one variable (temp)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> filter: removed 8,353 rows (97%), 243 rows remaining
#> rename: renamed one variable (temp)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> filter: removed 8,331 rows (97%), 265 rows remaining
#> rename: renamed one variable (temp)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> filter: removed 8,397 rows (98%), 199 rows remaining
#> rename: renamed one variable (temp)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> filter: removed 8,274 rows (96%), 322 rows remaining
#> rename: renamed one variable (temp)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> filter: removed 8,337 rows (97%), 259 rows remaining
#> rename: renamed one variable (temp)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> filter: removed 8,357 rows (97%), 239 rows remaining
#> rename: renamed one variable (temp)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> filter: removed 8,335 rows (97%), 261 rows remaining
#> rename: renamed one variable (temp)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> filter: removed 8,290 rows (96%), 306 rows remaining
#> rename: renamed one variable (temp)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> filter: removed 8,258 rows (96%), 338 rows remaining
#> rename: renamed one variable (temp)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> filter: removed 8,199 rows (95%), 397 rows remaining
#> rename: renamed one variable (temp)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> filter: removed 8,199 rows (95%), 397 rows remaining
#> rename: renamed one variable (temp)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> filter: removed 8,190 rows (95%), 406 rows remaining
#> rename: renamed one variable (temp)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> filter: removed 8,194 rows (95%), 402 rows remaining
#> rename: renamed one variable (temp)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> filter: removed 8,173 rows (95%), 423 rows remaining
#> rename: renamed one variable (temp)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> filter: removed 8,240 rows (96%), 356 rows remaining
#> rename: renamed one variable (temp)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> filter: removed 8,180 rows (95%), 416 rows remaining
#> rename: renamed one variable (temp)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> filter: removed 8,212 rows (96%), 384 rows remaining
#> rename: renamed one variable (temp)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> filter: removed 8,193 rows (95%), 403 rows remaining
#> rename: renamed one variable (temp)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> filter: removed 8,151 rows (95%), 445 rows remaining
#> rename: renamed one variable (temp)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> filter: removed 8,126 rows (95%), 470 rows remaining
#> rename: renamed one variable (temp)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> filter: removed 8,130 rows (95%), 466 rows remaining
#> rename: renamed one variable (temp)
#> mutate: new variable 'year' (character) with one unique value and 0% NA
#> filter: removed 8,386 rows (98%), 210 rows remaining
#> rename: renamed one variable (temp)
#> mutate: new variable 'year' (character) with one unique value and 0% NA

# Now create a data frame from the list of all annual values
big_dat_temp <- dplyr::bind_rows(data_list)
big_raster_dat_temp <- dplyr::bind_rows(raster_list)

big_dat_temp %>% drop_na(temp) %>% summarise(max = max(temp))
#> drop_na: no rows removed
#> summarise: now one row and one column, ungrouped
#> # A tibble: 1 × 1
#>     max
#>   <dbl>
#> 1  14.4
big_dat_temp %>% drop_na(temp) %>% summarise(min = min(temp))
#> drop_na: no rows removed
#> summarise: now one row and one column, ungrouped
#> # A tibble: 1 × 1
#>     min
#>   <dbl>
#> 1  2.96

# Plot data, looks like there's big inter-annual variation but a positive trend
big_raster_dat_temp %>%
  group_by(year) %>%
  drop_na(temp) %>%
  summarise(mean_temp = mean(temp)) %>%
  mutate(year_num = as.numeric(year)) %>%
  ggplot(., aes(year_num, mean_temp)) +
  geom_point(size = 2) +
  stat_smooth(method = "lm")
#> group_by: one grouping variable (year)
#> drop_na (grouped): no rows removed
#> summarise: now 27 rows and 2 columns, ungrouped
#> mutate: new variable 'year_num' (double) with 27 unique values and 0% NA
#> `geom_smooth()` using formula 'y ~ x'


# Now add in the new temperature column in the original data:
str(d_sub_temp)
#> tibble [8,596 × 14] (S3: tbl_df/tbl/data.frame)
#>  $ density    : num [1:8596] 158.15 10.33 5.26 19.55 812.16 ...
#>  $ year       : int [1:8596] 1993 1993 1993 1993 1993 1993 1993 1993 1993 1993 ...
#>  $ lat        : num [1:8596] 54.7 54.5 54.5 54.5 54.7 ...
#>  $ lon        : num [1:8596] 13 14.3 14.2 14 13.1 ...
#>  $ quarter    : int [1:8596] 1 1 1 1 1 1 1 1 1 1 ...
#>  $ haul.id    : chr [1:8596] "1993:1:GFR:SOL:H20:21:1" "1993:1:GFR:SOL:H20:22:32" "1993:1:GFR:SOL:H20:23:31" "1993:1:GFR:SOL:H20:24:30" ...
#>  $ IDx        : chr [1:8596] "1993.1.GFR.06S1.H20.21.1" "1993.1.GFR.06S1.H20.22.32" "1993.1.GFR.06S1.H20.23.31" "1993.1.GFR.06S1.H20.24.30" ...
#>  $ ices_rect  : chr [1:8596] "38G3" "38G4" "38G4" "37G3" ...
#>  $ sub_div    : chr [1:8596] "24" "24" "24" "24" ...
#>  $ density_fle: num [1:8596] 19.26 13.7 1.99 3.89 34.26 ...
#>  $ depth      : num [1:8596] 8.87 6.87 7.63 5.62 12.87 ...
#>  $ id_oxy     : chr [1:8596] "1993_1_13_54.6833" "1993_1_14.2667_54.5167" "1993_1_14.15_54.5167" "1993_1_13.9833_54.4833" ...
#>  $ oxy        : num [1:8596] 8.52 8.91 8.91 8.9 8.45 ...
#>  $ year_f     : Factor w/ 27 levels "1993","1994",..: 1 1 1 1 1 1 1 1 1 1 ...
str(big_dat_temp)
#> tibble [8,596 × 4] (S3: tbl_df/tbl/data.frame)
#>  $ lon : num [1:8596] 13 14.3 14.2 14 13.1 ...
#>  $ lat : num [1:8596] 54.7 54.5 54.5 54.5 54.7 ...
#>  $ temp: num [1:8596] 7.92 7.4 7.4 7.35 8.12 ...
#>  $ year: chr [1:8596] "1993" "1993" "1993" "1993" ...

# Create an ID for matching the temperature data with the cpue data
dat$id_temp <- paste(dat$year, dat$lon, dat$lat, sep = "_")
big_dat_temp$id_temp <- paste(big_dat_temp$year, big_dat_temp$lon, big_dat_temp$lat, sep = "_")

# Which id's are not in the cpue data (dat)? (It's because I don't have those years, not about the location)
ids <- dat$id_temp[!dat$id_temp %in% c(big_dat_temp$id_temp)]

unique(ids)
#> character(0)

# Select only the columns we want to merge
big_dat_sub_temp <- big_dat_temp %>% dplyr::select(id_temp, temp)

# Remove duplicate ID (one temp value per id)
big_dat_sub_temp2 <- big_dat_sub_temp %>% distinct(id_temp, .keep_all = TRUE)
#> distinct: removed 88 rows (1%), 8,508 rows remaining

# Join the data with raster-derived oxygen with the full cpue data
dat <- left_join(dat, big_dat_sub_temp2, by = "id_temp")
#> left_join: added one column (temp)
#>            > rows only in x       0
#>            > rows only in y  (    0)
#>            > matched rows     8,596
#>            >                 =======
#>            > rows total       8,596

colnames(dat)
#>  [1] "density"     "year"        "lat"         "lon"         "quarter"    
#>  [6] "haul.id"     "IDx"         "ices_rect"   "sub_div"     "density_fle"
#> [11] "depth"       "id_oxy"      "oxy"         "id_temp"     "temp"

dat <- dat %>% dplyr::select(-id_temp, -id_oxy)

# Drop NA temp
dat <- dat %>% drop_na(temp)
#> drop_na: no rows removed

# Now, if quarter == 1 in dat, temperature should be NA beucase I haven't done temperature for q1 here (see comment in the description of this script)
sort(unique(filter(dat, quarter == 1)$temp))
#> filter: removed 3,452 rows (40%), 5,144 rows remaining
#>    [1]  2.958224  2.977907  2.993523  3.223797  3.256330  3.374446  3.385705
#>    [8]  3.476515  3.549009  3.578635  3.609479  3.612852  3.614922  3.617384
#>   [15]  3.617421  3.636575  3.636666  3.639292  3.652544  3.710982  3.719994
#>   [22]  3.747388  3.748731  3.749818  3.778201  3.792435  3.797669  3.798714
#>   [29]  3.816328  3.832944  3.834569  3.836870  3.844881  3.857099  3.870043
#>   [36]  3.875435  3.877317  3.899827  3.911177  3.937457  3.950228  3.962593
#>   [43]  3.969385  3.971485  3.982919  3.989504  3.994432  3.997744  3.998825
#>   [50]  4.004031  4.014246  4.014594  4.017230  4.026310  4.028686  4.031924
#>   [57]  4.044921  4.046828  4.055898  4.079192  4.079518  4.082264  4.090783
#>   [64]  4.096813  4.097252  4.097594  4.097764  4.104354  4.105478  4.105582
#>   [71]  4.114230  4.118768  4.122777  4.125894  4.126760  4.126840  4.132259
#>   [78]  4.133278  4.137680  4.143962  4.147805  4.148928  4.160384  4.162132
#>   [85]  4.164441  4.169403  4.171877  4.172824  4.173273  4.176000  4.181066
#>   [92]  4.189746  4.191610  4.194535  4.199857  4.200024  4.200540  4.200954
#>   [99]  4.202446  4.203196  4.203223  4.207467  4.207693  4.208196  4.209729
#>  [106]  4.211332  4.218529  4.218883  4.221886  4.233080  4.233312  4.233783
#>  [113]  4.234293  4.234752  4.241692  4.246612  4.248327  4.250917  4.251680
#>  [120]  4.253088  4.259002  4.262193  4.272968  4.276708  4.278452  4.283557
#>  [127]  4.285990  4.287835  4.294793  4.296268  4.303352  4.309692  4.310034
#>  [134]  4.313739  4.317819  4.324457  4.325772  4.331237  4.336834  4.341463
#>  [141]  4.343214  4.344606  4.345228  4.348113  4.351997  4.353914  4.354610
#>  [148]  4.356189  4.359114  4.359272  4.362440  4.367089  4.367374  4.372076
#>  [155]  4.374164  4.376568  4.376785  4.377234  4.377676  4.379583  4.385276
#>  [162]  4.386055  4.386161  4.387650  4.393044  4.396782  4.397075  4.404854
#>  [169]  4.411055  4.411690  4.415215  4.415274  4.417570  4.417683  4.420362
#>  [176]  4.422089  4.424385  4.426818  4.428303  4.435161  4.437474  4.440338
#>  [183]  4.440590  4.440610  4.444174  4.444708  4.448224  4.448498  4.449073
#>  [190]  4.450647  4.454594  4.454720  4.455385  4.455431  4.456888  4.460278
#>  [197]  4.461328  4.461499  4.461659  4.465495  4.465783  4.466074  4.469590
#>  [204]  4.472388  4.474185  4.475486  4.477030  4.479476  4.489424  4.491337
#>  [211]  4.497600  4.499194  4.501830  4.506042  4.509599  4.510136  4.511997
#>  [218]  4.513574  4.515757  4.516190  4.517871  4.520576  4.526393  4.527307
#>  [225]  4.531136  4.532010  4.535109  4.535235  4.535362  4.539148  4.545288
#>  [232]  4.548208  4.550722  4.551098  4.552190  4.557715  4.558985  4.559113
#>  [239]  4.562158  4.563726  4.564559  4.564785  4.568252  4.569945  4.570329
#>  [246]  4.571776  4.579925  4.580394  4.582285  4.590286  4.592607  4.594835
#>  [253]  4.596705  4.598000  4.598871  4.600291  4.602280  4.603179  4.606045
#>  [260]  4.610318  4.610648  4.611733  4.613242  4.614636  4.615299  4.616702
#>  [267]  4.616946  4.619780  4.623169  4.623742  4.624599  4.624719  4.625456
#>  [274]  4.627881  4.628745  4.629218  4.629726  4.633007  4.636055  4.642022
#>  [281]  4.648447  4.649889  4.652298  4.653326  4.653754  4.656019  4.657910
#>  [288]  4.658260  4.659263  4.659548  4.659866  4.661207  4.662169  4.662864
#>  [295]  4.664120  4.664762  4.664841  4.667338  4.670696  4.674144  4.674237
#>  [302]  4.679176  4.679704  4.679973  4.681125  4.681692  4.682104  4.682850
#>  [309]  4.683049  4.685088  4.688885  4.689728  4.691052  4.693350  4.696641
#>  [316]  4.697520  4.702357  4.703799  4.706959  4.707108  4.708236  4.708596
#>  [323]  4.713647  4.716789  4.727227  4.730815  4.738587  4.739919  4.740427
#>  [330]  4.741619  4.742243  4.742613  4.744205  4.746566  4.747887  4.748156
#>  [337]  4.749302  4.750839  4.751103  4.751692  4.756472  4.758022  4.760124
#>  [344]  4.761899  4.765853  4.772694  4.773114  4.777049  4.777371  4.780346
#>  [351]  4.784943  4.788002  4.789382  4.793611  4.794100  4.794329  4.796908
#>  [358]  4.797589  4.797787  4.797851  4.799006  4.801819  4.802157  4.802619
#>  [365]  4.807421  4.807609  4.808099  4.809225  4.809879  4.810145  4.811039
#>  [372]  4.816127  4.816136  4.818380  4.818443  4.818457  4.819663  4.821506
#>  [379]  4.822948  4.824905  4.826361  4.826866  4.827997  4.828654  4.828983
#>  [386]  4.829228  4.838700  4.839209  4.841633  4.843996  4.845621  4.849431
#>  [393]  4.849768  4.852057  4.852392  4.854387  4.860249  4.861147  4.861279
#>  [400]  4.865572  4.866503  4.869356  4.870738  4.870881  4.872573  4.873811
#>  [407]  4.874122  4.876288  4.876597  4.877438  4.878028  4.882408  4.883814
#>  [414]  4.884378  4.885682  4.886443  4.886504  4.888556  4.890750  4.890959
#>  [421]  4.894034  4.894123  4.896674  4.897063  4.897257  4.901628  4.903557
#>  [428]  4.904322  4.905144  4.906490  4.911409  4.911666  4.913845  4.915731
#>  [435]  4.916366  4.918209  4.919784  4.921180  4.921583  4.927150  4.933301
#>  [442]  4.933584  4.935528  4.935556  4.937133  4.937287  4.939657  4.941129
#>  [449]  4.941728  4.942205  4.944828  4.945295  4.947072  4.947836  4.948660
#>  [456]  4.949219  4.950247  4.951689  4.951871  4.953047  4.954143  4.955868
#>  [463]  4.957600  4.957810  4.958304  4.960976  4.961637  4.963966  4.965452
#>  [470]  4.966080  4.967572  4.969876  4.970377  4.974637  4.977271  4.978258
#>  [477]  4.979991  4.982078  4.982852  4.984522  4.985198  4.987780  4.989056
#>  [484]  4.990435  4.990997  4.992731  4.993720  4.993855  4.995569  4.998511
#>  [491]  4.998547  5.002637  5.003149  5.003733  5.003943  5.005530  5.006228
#>  [498]  5.008610  5.008631  5.010148  5.010753  5.012366  5.013330  5.013639
#>  [505]  5.015437  5.015530  5.019417  5.019793  5.020920  5.022175  5.022441
#>  [512]  5.022651  5.026094  5.026546  5.029140  5.029842  5.031149  5.032935
#>  [519]  5.033037  5.033305  5.033937  5.034018  5.035511  5.035673  5.038367
#>  [526]  5.042373  5.045136  5.045504  5.045645  5.047283  5.047638  5.048657
#>  [533]  5.049548  5.050296  5.051687  5.055639  5.056394  5.058420  5.058541
#>  [540]  5.060045  5.060093  5.060446  5.061408  5.062121  5.063489  5.063675
#>  [547]  5.065036  5.066417  5.067536  5.067731  5.067914  5.067932  5.068798
#>  [554]  5.070262  5.071109  5.071707  5.071931  5.073218  5.073225  5.074499
#>  [561]  5.074967  5.075733  5.077461  5.077487  5.079814  5.082135  5.084095
#>  [568]  5.085873  5.086342  5.087391  5.087948  5.089399  5.090387  5.091367
#>  [575]  5.093070  5.093159  5.094100  5.095073  5.098520  5.098800  5.099684
#>  [582]  5.100171  5.100228  5.104960  5.107722  5.110296  5.111514  5.113251
#>  [589]  5.114032  5.117215  5.117966  5.118291  5.118953  5.121449  5.126131
#>  [596]  5.127427  5.129092  5.129124  5.129271  5.135248  5.135618  5.136453
#>  [603]  5.136798  5.136905  5.142294  5.143530  5.144002  5.145110  5.145737
#>  [610]  5.145782  5.146523  5.150678  5.154189  5.155632  5.156444  5.156450
#>  [617]  5.157687  5.158981  5.161004  5.163614  5.164062  5.165675  5.168256
#>  [624]  5.168467  5.169337  5.169947  5.176031  5.178155  5.179928  5.180344
#>  [631]  5.181737  5.187409  5.187889  5.187897  5.188848  5.189964  5.195452
#>  [638]  5.199189  5.200465  5.200929  5.207057  5.210444  5.215236  5.216211
#>  [645]  5.217161  5.219773  5.221550  5.223249  5.226219  5.227899  5.230002
#>  [652]  5.231825  5.234433  5.235566  5.236813  5.243919  5.246905  5.246984
#>  [659]  5.247726  5.250586  5.254230  5.254714  5.255669  5.255924  5.256048
#>  [666]  5.260777  5.261634  5.262286  5.265271  5.265364  5.267058  5.268578
#>  [673]  5.268715  5.270987  5.271719  5.273154  5.274882  5.276154  5.277490
#>  [680]  5.277540  5.281136  5.281552  5.284024  5.286290  5.287147  5.289564
#>  [687]  5.294180  5.294564  5.296088  5.299078  5.299281  5.300562  5.301988
#>  [694]  5.303634  5.304953  5.305533  5.307549  5.308039  5.308215  5.312601
#>  [701]  5.313295  5.315128  5.316437  5.317792  5.318651  5.318748  5.322125
#>  [708]  5.322251  5.322833  5.326457  5.327368  5.328738  5.330262  5.331277
#>  [715]  5.331681  5.334774  5.336367  5.337161  5.339528  5.339641  5.343005
#>  [722]  5.348794  5.351279  5.351572  5.353373  5.357416  5.359153  5.361931
#>  [729]  5.363724  5.368643  5.371366  5.372745  5.374075  5.378957  5.380451
#>  [736]  5.382568  5.383985  5.386483  5.386769  5.389111  5.391350  5.392283
#>  [743]  5.393130  5.393532  5.394609  5.399582  5.399715  5.401700  5.404705
#>  [750]  5.410907  5.411271  5.411361  5.411764  5.413438  5.413850  5.414216
#>  [757]  5.414345  5.414417  5.414899  5.415986  5.417235  5.417615  5.418753
#>  [764]  5.423094  5.423946  5.426663  5.427235  5.429080  5.429274  5.430217
#>  [771]  5.431508  5.436378  5.436679  5.439868  5.439904  5.442746  5.445337
#>  [778]  5.448015  5.451237  5.451391  5.452722  5.454193  5.455212  5.456333
#>  [785]  5.456852  5.457504  5.460073  5.460582  5.461035  5.464107  5.472032
#>  [792]  5.473087  5.473130  5.473369  5.477180  5.478262  5.480734  5.481699
#>  [799]  5.485290  5.486989  5.487034  5.489468  5.490311  5.493862  5.494154
#>  [806]  5.494403  5.495753  5.498800  5.499231  5.499938  5.501977  5.504883
#>  [813]  5.505348  5.506943  5.507303  5.508615  5.510536  5.515858  5.518802
#>  [820]  5.519865  5.530003  5.530090  5.530973  5.534888  5.536977  5.542958
#>  [827]  5.543382  5.548210  5.549942  5.550576  5.555026  5.557985  5.558899
#>  [834]  5.559251  5.561673  5.563814  5.565254  5.566564  5.567732  5.570850
#>  [841]  5.571259  5.571443  5.572601  5.573089  5.575017  5.581186  5.583850
#>  [848]  5.589142  5.590179  5.591698  5.591864  5.594252  5.594937  5.595773
#>  [855]  5.596577  5.596632  5.598547  5.599382  5.601964  5.602715  5.606315
#>  [862]  5.607392  5.608490  5.609995  5.611064  5.611847  5.613574  5.614026
#>  [869]  5.615580  5.617079  5.617704  5.618550  5.620138  5.622767  5.624443
#>  [876]  5.627008  5.631640  5.639663  5.640270  5.641012  5.641433  5.642631
#>  [883]  5.645315  5.652018  5.653055  5.659804  5.661085  5.663149  5.665814
#>  [890]  5.666595  5.667602  5.670241  5.671516  5.672647  5.673465  5.675957
#>  [897]  5.676814  5.677598  5.679912  5.682577  5.682603  5.682851  5.689855
#>  [904]  5.690326  5.691455  5.693165  5.693597  5.694212  5.695802  5.696524
#>  [911]  5.697221  5.698368  5.699171  5.704037  5.705319  5.705443  5.705953
#>  [918]  5.706450  5.710804  5.711708  5.712236  5.712527  5.714056  5.715807
#>  [925]  5.716017  5.717116  5.717404  5.718410  5.719711  5.720504  5.721099
#>  [932]  5.723513  5.724210  5.726231  5.726897  5.728151  5.729407  5.730013
#>  [939]  5.730201  5.734772  5.738698  5.741793  5.744223  5.745812  5.746220
#>  [946]  5.747474  5.749218  5.750917  5.752106  5.752299  5.753626  5.754772
#>  [953]  5.756701  5.760917  5.762501  5.765739  5.766826  5.767228  5.767378
#>  [960]  5.768004  5.770489  5.771153  5.773159  5.774144  5.777538  5.777723
#>  [967]  5.778889  5.780757  5.783123  5.783278  5.785026  5.791028  5.792243
#>  [974]  5.795360  5.796718  5.797112  5.797367  5.798758  5.801220  5.801965
#>  [981]  5.804833  5.806046  5.807935  5.808171  5.809918  5.810248  5.810855
#>  [988]  5.812171  5.812673  5.812845  5.814827  5.816434  5.819524  5.819952
#>  [995]  5.820638  5.827460  5.830982  5.832360  5.835048  5.835373  5.836885
#> [1002]  5.836920  5.837160  5.837407  5.837520  5.838791  5.841054  5.841108
#> [1009]  5.841123  5.844155  5.845769  5.848083  5.849301  5.853338  5.855472
#> [1016]  5.857036  5.857576  5.860630  5.860894  5.868212  5.868552  5.869299
#> [1023]  5.869762  5.869899  5.876530  5.878141  5.878891  5.880136  5.881585
#> [1030]  5.885845  5.887106  5.887157  5.887887  5.888371  5.888813  5.889399
#> [1037]  5.889442  5.890186  5.890908  5.890994  5.891494  5.891886  5.892009
#> [1044]  5.892541  5.892990  5.894333  5.895605  5.895711  5.896341  5.897089
#> [1051]  5.898446  5.900169  5.904375  5.906166  5.906266  5.908676  5.910352
#> [1058]  5.911232  5.911880  5.915004  5.915198  5.916363  5.917019  5.917744
#> [1065]  5.920003  5.921183  5.923377  5.924506  5.924609  5.926306  5.929543
#> [1072]  5.930396  5.930544  5.930730  5.936229  5.939440  5.940049  5.943094
#> [1079]  5.943562  5.945342  5.946637  5.946778  5.948162  5.948387  5.948850
#> [1086]  5.949485  5.952791  5.953255  5.958603  5.960401  5.962445  5.963141
#> [1093]  5.963913  5.963939  5.964945  5.966572  5.969190  5.969456  5.971532
#> [1100]  5.973176  5.974835  5.976651  5.976678  5.978564  5.978686  5.979802
#> [1107]  5.982229  5.983575  5.984402  5.985241  5.986529  5.986828  5.987286
#> [1114]  5.988816  5.990800  5.991488  5.991668  5.993280  5.994778  5.995858
#> [1121]  5.996173  5.996894  5.998541  5.999273  5.999749  6.000316  6.000683
#> [1128]  6.002070  6.002974  6.003697  6.004939  6.005089  6.007521  6.009389
#> [1135]  6.009805  6.009905  6.010267  6.011360  6.011480  6.013667  6.013815
#> [1142]  6.015047  6.015570  6.018139  6.019862  6.021156  6.021446  6.022891
#> [1149]  6.024850  6.026813  6.027366  6.027926  6.028098  6.033516  6.036137
#> [1156]  6.036423  6.037479  6.038026  6.038713  6.041906  6.044592  6.048471
#> [1163]  6.049976  6.052257  6.052895  6.053431  6.060360  6.061023  6.063694
#> [1170]  6.064002  6.066262  6.067760  6.068665  6.069285  6.069308  6.069544
#> [1177]  6.070933  6.072370  6.073565  6.073956  6.074131  6.074272  6.075572
#> [1184]  6.077366  6.078971  6.081289  6.082976  6.085542  6.089669  6.090763
#> [1191]  6.090905  6.091777  6.092230  6.093967  6.095300  6.095550  6.096699
#> [1198]  6.097441  6.099595  6.102215  6.102528  6.103856  6.107648  6.107785
#> [1205]  6.108607  6.109690  6.110367  6.111166  6.111896  6.112983  6.114180
#> [1212]  6.114933  6.115752  6.116329  6.116742  6.117027  6.119735  6.119837
#> [1219]  6.120508  6.121333  6.121414  6.123997  6.124766  6.125810  6.126186
#> [1226]  6.126229  6.126381  6.127150  6.127500  6.127992  6.130125  6.131109
#> [1233]  6.131161  6.132392  6.132667  6.133738  6.134476  6.135193  6.135637
#> [1240]  6.135852  6.136106  6.137562  6.139073  6.140927  6.144809  6.145085
#> [1247]  6.146022  6.146424  6.146477  6.147291  6.149210  6.150610  6.151023
#> [1254]  6.151807  6.153386  6.153591  6.153714  6.154435  6.155209  6.155335
#> [1261]  6.156005  6.156132  6.158905  6.158906  6.160147  6.160443  6.160746
#> [1268]  6.160758  6.162627  6.164896  6.165036  6.166529  6.168427  6.168787
#> [1275]  6.170653  6.171834  6.171972  6.172163  6.172687  6.174691  6.182020
#> [1282]  6.182372  6.182778  6.185467  6.185721  6.186479  6.186706  6.187916
#> [1289]  6.190440  6.191139  6.192104  6.192608  6.192645  6.193485  6.194785
#> [1296]  6.195986  6.197603  6.197711  6.198424  6.199453  6.199866  6.202178
#> [1303]  6.202822  6.204601  6.205502  6.207626  6.207710  6.210309  6.212716
#> [1310]  6.213832  6.214057  6.214428  6.215680  6.217749  6.231209  6.231847
#> [1317]  6.233350  6.233891  6.245624  6.246075  6.246192  6.247458  6.249233
#> [1324]  6.249629  6.251263  6.252907  6.254701  6.256027  6.256578  6.258843
#> [1331]  6.259224  6.259402  6.259860  6.260212  6.261230  6.262694  6.263152
#> [1338]  6.264690  6.264897  6.265204  6.266847  6.268336  6.268384  6.273526
#> [1345]  6.274201  6.275483  6.276874  6.277062  6.280899  6.281283  6.283429
#> [1352]  6.283978  6.287638  6.290700  6.292484  6.293771  6.295752  6.297017
#> [1359]  6.297148  6.300585  6.301394  6.301653  6.302081  6.302221  6.302587
#> [1366]  6.303882  6.304069  6.304519  6.305752  6.305832  6.307802  6.308232
#> [1373]  6.308446  6.309993  6.311543  6.312216  6.313103  6.315886  6.316274
#> [1380]  6.317898  6.318203  6.320136  6.322520  6.322802  6.325462  6.327612
#> [1387]  6.327898  6.329858  6.331962  6.332026  6.332499  6.332675  6.334339
#> [1394]  6.334480  6.334490  6.337262  6.338270  6.338439  6.339072  6.339407
#> [1401]  6.339876  6.342721  6.343276  6.344070  6.349693  6.351007  6.351569
#> [1408]  6.351890  6.352252  6.352465  6.354615  6.356928  6.356945  6.357724
#> [1415]  6.357868  6.357958  6.359429  6.364362  6.364767  6.364823  6.366251
#> [1422]  6.369137  6.370618  6.371607  6.372056  6.372289  6.373075  6.373528
#> [1429]  6.375979  6.378183  6.379352  6.379443  6.380764  6.382856  6.384225
#> [1436]  6.384666  6.385434  6.385797  6.387736  6.393811  6.393926  6.395063
#> [1443]  6.395970  6.398478  6.400103  6.403574  6.404089  6.404120  6.405373
#> [1450]  6.406724  6.406865  6.407238  6.407594  6.408187  6.408456  6.409238
#> [1457]  6.409952  6.410185  6.410550  6.410665  6.411076  6.411172  6.413823
#> [1464]  6.415113  6.416141  6.416608  6.417020  6.417266  6.417372  6.417427
#> [1471]  6.417478  6.417503  6.418444  6.418703  6.420941  6.422152  6.423143
#> [1478]  6.423374  6.424793  6.424909  6.424964  6.425001  6.428039  6.429341
#> [1485]  6.432403  6.433790  6.435753  6.436103  6.436550  6.436789  6.437915
#> [1492]  6.438594  6.438632  6.438922  6.439289  6.439368  6.440225  6.444253
#> [1499]  6.444306  6.448311  6.449595  6.450707  6.451582  6.452478  6.453504
#> [1506]  6.453506  6.455028  6.455487  6.456474  6.456820  6.457419  6.457528
#> [1513]  6.457655  6.458038  6.464113  6.464132  6.466211  6.467654  6.468113
#> [1520]  6.468567  6.468608  6.470221  6.476710  6.477295  6.477715  6.480218
#> [1527]  6.481098  6.484308  6.484678  6.484982  6.487278  6.489201  6.489296
#> [1534]  6.489372  6.489565  6.490274  6.492757  6.492810  6.498848  6.499422
#> [1541]  6.500195  6.503692  6.504893  6.505198  6.505964  6.506003  6.506529
#> [1548]  6.507507  6.510317  6.510840  6.510909  6.513266  6.513364  6.513513
#> [1555]  6.515276  6.516397  6.519515  6.521350  6.521460  6.522155  6.523247
#> [1562]  6.523834  6.524462  6.524850  6.529837  6.535040  6.535944  6.536695
#> [1569]  6.537813  6.539875  6.540478  6.540570  6.542596  6.543564  6.543578
#> [1576]  6.544954  6.545041  6.545653  6.546008  6.546400  6.552684  6.552847
#> [1583]  6.553938  6.554682  6.555714  6.555994  6.558390  6.558837  6.558993
#> [1590]  6.563165  6.564752  6.566312  6.566338  6.571453  6.572206  6.574914
#> [1597]  6.575534  6.576743  6.577611  6.577665  6.578326  6.578501  6.579071
#> [1604]  6.579500  6.580424  6.580905  6.586762  6.587501  6.588930  6.589015
#> [1611]  6.589180  6.590409  6.592033  6.592881  6.595714  6.596331  6.597219
#> [1618]  6.599064  6.602086  6.605143  6.605962  6.606520  6.606924  6.610958
#> [1625]  6.611138  6.611442  6.612448  6.616620  6.616750  6.616863  6.616937
#> [1632]  6.617424  6.618023  6.619967  6.620443  6.621943  6.622728  6.626179
#> [1639]  6.626210  6.626952  6.629479  6.629768  6.630107  6.630984  6.632145
#> [1646]  6.632627  6.635000  6.637152  6.637451  6.637692  6.638523  6.639009
#> [1653]  6.639110  6.640406  6.641918  6.642920  6.642982  6.643717  6.647377
#> [1660]  6.648898  6.650080  6.651973  6.652941  6.653483  6.655153  6.655363
#> [1667]  6.655759  6.655891  6.659892  6.660906  6.661238  6.661303  6.663285
#> [1674]  6.663386  6.665982  6.666120  6.666124  6.667306  6.671828  6.672165
#> [1681]  6.674324  6.674754  6.675793  6.676349  6.683213  6.686034  6.686996
#> [1688]  6.687197  6.690218  6.692649  6.692917  6.695391  6.697828  6.701499
#> [1695]  6.702052  6.702285  6.703720  6.704316  6.704427  6.704471  6.704547
#> [1702]  6.705232  6.706994  6.708382  6.712039  6.714230  6.718328  6.718469
#> [1709]  6.719164  6.719700  6.723325  6.724998  6.726837  6.728148  6.728254
#> [1716]  6.728259  6.730650  6.733731  6.733891  6.734101  6.734394  6.734423
#> [1723]  6.735197  6.735559  6.736317  6.738045  6.738634  6.740670  6.740981
#> [1730]  6.742012  6.743486  6.744083  6.744385  6.744852  6.745073  6.746460
#> [1737]  6.746864  6.747184  6.747890  6.748295  6.748641  6.749410  6.751269
#> [1744]  6.752146  6.752388  6.753726  6.754533  6.754867  6.755127  6.756304
#> [1751]  6.756410  6.760190  6.763630  6.764674  6.764850  6.767937  6.768255
#> [1758]  6.770282  6.771778  6.775987  6.777364  6.777821  6.779987  6.780158
#> [1765]  6.781734  6.782207  6.783389  6.783589  6.786464  6.788209  6.789391
#> [1772]  6.790153  6.790907  6.792021  6.792198  6.793345  6.793560  6.794409
#> [1779]  6.794492  6.796542  6.797789  6.798141  6.798187  6.798320  6.799073
#> [1786]  6.800642  6.801268  6.801529  6.801831  6.803039  6.803177  6.803403
#> [1793]  6.804984  6.806819  6.806938  6.807514  6.812205  6.812434  6.814649
#> [1800]  6.817882  6.818442  6.821235  6.822359  6.823004  6.828066  6.828204
#> [1807]  6.830703  6.830761  6.831105  6.832827  6.834746  6.836166  6.837851
#> [1814]  6.842074  6.845862  6.851820  6.853402  6.855119  6.858102  6.859114
#> [1821]  6.860374  6.860420  6.861689  6.862853  6.863419  6.865933  6.866409
#> [1828]  6.868402  6.868771  6.870012  6.872545  6.872556  6.873087  6.877382
#> [1835]  6.877740  6.878436  6.878489  6.878966  6.879300  6.879707  6.884423
#> [1842]  6.884438  6.884725  6.886150  6.886364  6.890347  6.891070  6.891570
#> [1849]  6.892450  6.893573  6.895074  6.897002  6.899908  6.905284  6.906092
#> [1856]  6.911406  6.911534  6.914028  6.916000  6.917246  6.917761  6.918889
#> [1863]  6.919200  6.920221  6.922799  6.924845  6.924960  6.926153  6.928931
#> [1870]  6.931368  6.931452  6.931976  6.932678  6.933942  6.937644  6.938505
#> [1877]  6.938991  6.940276  6.946567  6.948700  6.949386  6.950622  6.951291
#> [1884]  6.953299  6.953600  6.954710  6.956544  6.957518  6.958011  6.959459
#> [1891]  6.960108  6.960585  6.961947  6.964950  6.967000  6.967337  6.968639
#> [1898]  6.969792  6.972832  6.975478  6.976183  6.979264  6.981365  6.983374
#> [1905]  6.984246  6.984895  6.986898  6.987198  6.987787  6.990986  6.995474
#> [1912]  6.995481  6.996006  6.996317  6.997208  7.002831  7.003145  7.004928
#> [1919]  7.005953  7.006073  7.006561  7.007248  7.007534  7.008497  7.009960
#> [1926]  7.010326  7.010338  7.010405  7.011337  7.013827  7.015499  7.015834
#> [1933]  7.018231  7.018351  7.019208  7.022678  7.022726  7.022932  7.023682
#> [1940]  7.027664  7.030758  7.033125  7.034165  7.034770  7.035062  7.038103
#> [1947]  7.039128  7.041061  7.041190  7.041553  7.045038  7.046327  7.047804
#> [1954]  7.048823  7.051275  7.052446  7.053666  7.055801  7.055877  7.056861
#> [1961]  7.057234  7.058488  7.060075  7.060758  7.061179  7.062578  7.062818
#> [1968]  7.063263  7.063958  7.065172  7.066753  7.070311  7.070991  7.075772
#> [1975]  7.076079  7.076506  7.077032  7.077952  7.079672  7.079768  7.082922
#> [1982]  7.084627  7.084778  7.085793  7.085793  7.086592  7.087843  7.088502
#> [1989]  7.089076  7.092359  7.092456  7.093776  7.094291  7.095784  7.095787
#> [1996]  7.097786  7.098083  7.101612  7.101735  7.102999  7.103345  7.106011
#> [2003]  7.106682  7.106747  7.108578  7.110430  7.112132  7.114345  7.118812
#> [2010]  7.119021  7.119617  7.120064  7.120153  7.122223  7.122541  7.122616
#> [2017]  7.125860  7.126030  7.126615  7.128305  7.132336  7.133020  7.133459
#> [2024]  7.133841  7.134625  7.134729  7.135583  7.135730  7.138982  7.140463
#> [2031]  7.141034  7.143214  7.143476  7.145005  7.146416  7.149567  7.149884
#> [2038]  7.150005  7.151894  7.153104  7.153297  7.153569  7.155355  7.155865
#> [2045]  7.157555  7.158687  7.162725  7.162921  7.163691  7.164043  7.167348
#> [2052]  7.169000  7.170976  7.171029  7.171292  7.175071  7.175281  7.177028
#> [2059]  7.178868  7.181715  7.181770  7.183211  7.185654  7.188098  7.189766
#> [2066]  7.190935  7.194403  7.199596  7.200718  7.201177  7.202879  7.203440
#> [2073]  7.206385  7.208260  7.209327  7.209841  7.212959  7.216625  7.217808
#> [2080]  7.219432  7.219947  7.220681  7.221314  7.222695  7.223204  7.223410
#> [2087]  7.225037  7.225585  7.225617  7.229116  7.229563  7.230498  7.230723
#> [2094]  7.231196  7.231521  7.232023  7.234436  7.235186  7.235197  7.236094
#> [2101]  7.236333  7.237220  7.238407  7.238538  7.239168  7.241282  7.242267
#> [2108]  7.244709  7.245120  7.245142  7.246563  7.247235  7.247776  7.247919
#> [2115]  7.248177  7.250122  7.253465  7.253688  7.253847  7.255956  7.258252
#> [2122]  7.258487  7.259732  7.260571  7.262137  7.262275  7.263376  7.264370
#> [2129]  7.268829  7.271002  7.271797  7.272354  7.273411  7.273590  7.277101
#> [2136]  7.278966  7.279615  7.279804  7.282843  7.285031  7.288036  7.291441
#> [2143]  7.291935  7.292780  7.293055  7.294488  7.295552  7.296659  7.297561
#> [2150]  7.299951  7.302293  7.303078  7.307274  7.308513  7.308989  7.309605
#> [2157]  7.311444  7.311536  7.312188  7.312585  7.312951  7.313697  7.313810
#> [2164]  7.315658  7.317474  7.317536  7.318760  7.318956  7.320715  7.320719
#> [2171]  7.324406  7.326539  7.327958  7.328712  7.330668  7.333970  7.335711
#> [2178]  7.336702  7.336863  7.337302  7.338826  7.338919  7.339141  7.340087
#> [2185]  7.341549  7.341574  7.347067  7.347900  7.350297  7.351873  7.353057
#> [2192]  7.353246  7.354227  7.354866  7.355914  7.356202  7.356396  7.356771
#> [2199]  7.359758  7.361071  7.361132  7.362216  7.362972  7.364073  7.364450
#> [2206]  7.364696  7.368046  7.368954  7.369723  7.371143  7.372979  7.373107
#> [2213]  7.373251  7.373909  7.375700  7.376214  7.376818  7.378398  7.378729
#> [2220]  7.381149  7.381189  7.382547  7.387276  7.389198  7.391708  7.392869
#> [2227]  7.393390  7.394284  7.395562  7.396564  7.397263  7.397590  7.398420
#> [2234]  7.399247  7.402232  7.402648  7.403462  7.404589  7.407028  7.407741
#> [2241]  7.410226  7.411820  7.413107  7.416266  7.416279  7.416750  7.417937
#> [2248]  7.418487  7.419285  7.420686  7.421381  7.421542  7.423670  7.426252
#> [2255]  7.426454  7.427531  7.427732  7.431891  7.431970  7.435643  7.435782
#> [2262]  7.437303  7.437591  7.438424  7.441065  7.443325  7.443886  7.444254
#> [2269]  7.445392  7.445794  7.447461  7.447679  7.448009  7.448127  7.449005
#> [2276]  7.450717  7.451852  7.453799  7.454748  7.456573  7.457313  7.458447
#> [2283]  7.459109  7.459356  7.460636  7.461431  7.462831  7.465206  7.465414
#> [2290]  7.466447  7.466664  7.468819  7.468955  7.470871  7.472214  7.472479
#> [2297]  7.474991  7.475831  7.476721  7.476905  7.477755  7.478224  7.481319
#> [2304]  7.482504  7.482888  7.482924  7.484300  7.485791  7.486409  7.487027
#> [2311]  7.487862  7.491651  7.497690  7.499640  7.501456  7.503775  7.504931
#> [2318]  7.506477  7.509622  7.511246  7.512588  7.514777  7.515653  7.516006
#> [2325]  7.516846  7.517285  7.517399  7.518932  7.519253  7.520347  7.521515
#> [2332]  7.522871  7.523004  7.524359  7.525097  7.525309  7.525640  7.526009
#> [2339]  7.528424  7.529705  7.530548  7.530554  7.530558  7.531459  7.531526
#> [2346]  7.535866  7.536091  7.536187  7.536189  7.539206  7.542466  7.542669
#> [2353]  7.542928  7.544596  7.546775  7.547709  7.551289  7.551348  7.555670
#> [2360]  7.557144  7.562500  7.564037  7.566671  7.566718  7.566789  7.567780
#> [2367]  7.568144  7.568162  7.570676  7.574837  7.576331  7.576569  7.579522
#> [2374]  7.581979  7.582950  7.583285  7.585407  7.586043  7.586306  7.587998
#> [2381]  7.590629  7.593833  7.596858  7.597494  7.598225  7.600238  7.600430
#> [2388]  7.600984  7.602269  7.602674  7.608132  7.611390  7.612964  7.613729
#> [2395]  7.614354  7.615674  7.617716  7.618595  7.619484  7.620725  7.622284
#> [2402]  7.622768  7.624570  7.624655  7.627137  7.627813  7.628368  7.632064
#> [2409]  7.632382  7.636333  7.636532  7.637109  7.637885  7.641257  7.641477
#> [2416]  7.645876  7.647939  7.648739  7.649559  7.650813  7.652867  7.659098
#> [2423]  7.660381  7.662102  7.664338  7.664653  7.665024  7.667476  7.669062
#> [2430]  7.669537  7.674215  7.674450  7.674604  7.674960  7.675408  7.675604
#> [2437]  7.676952  7.681285  7.682201  7.687331  7.691273  7.691460  7.692666
#> [2444]  7.692903  7.693644  7.694836  7.695371  7.697904  7.699045  7.699156
#> [2451]  7.699214  7.702024  7.703188  7.703443  7.704562  7.705085  7.705438
#> [2458]  7.708902  7.709036  7.710457  7.710609  7.712220  7.712775  7.714431
#> [2465]  7.715994  7.721903  7.723566  7.723627  7.725373  7.728230  7.729712
#> [2472]  7.733797  7.734875  7.735180  7.737081  7.737219  7.737420  7.738669
#> [2479]  7.739661  7.740087  7.740945  7.741337  7.743318  7.744926  7.745885
#> [2486]  7.746120  7.747360  7.747489  7.749288  7.750458  7.752392  7.755308
#> [2493]  7.756067  7.759521  7.760212  7.761615  7.761695  7.762207  7.762631
#> [2500]  7.764685  7.766316  7.767130  7.769093  7.769833  7.771335  7.772323
#> [2507]  7.772923  7.773054  7.774012  7.774724  7.774736  7.774775  7.776582
#> [2514]  7.779661  7.780040  7.780838  7.782129  7.783962  7.785405  7.787185
#> [2521]  7.787856  7.788432  7.788617  7.791043  7.792232  7.793174  7.793566
#> [2528]  7.795449  7.796513  7.797186  7.797609  7.797692  7.798954  7.800797
#> [2535]  7.802814  7.803642  7.807654  7.808991  7.814496  7.815516  7.815530
#> [2542]  7.817645  7.818104  7.822342  7.822539  7.826798  7.828425  7.829655
#> [2549]  7.830116  7.830188  7.830208  7.830889  7.831053  7.831417  7.832557
#> [2556]  7.833294  7.833667  7.834415  7.837135  7.838066  7.838365  7.838580
#> [2563]  7.838617  7.839098  7.839363  7.841273  7.841682  7.842811  7.842957
#> [2570]  7.843024  7.845321  7.846027  7.846622  7.847541  7.847919  7.851112
#> [2577]  7.852452  7.854008  7.854880  7.855500  7.859807  7.864640  7.867354
#> [2584]  7.867552  7.867620  7.868050  7.869011  7.871224  7.872242  7.874936
#> [2591]  7.875129  7.875676  7.875984  7.876012  7.877526  7.878604  7.881056
#> [2598]  7.881407  7.881810  7.883832  7.884351  7.884375  7.884828  7.886335
#> [2605]  7.887583  7.889802  7.890744  7.892756  7.893703  7.893770  7.893773
#> [2612]  7.894641  7.894771  7.895282  7.899872  7.900492  7.900981  7.903079
#> [2619]  7.903217  7.903403  7.903984  7.905301  7.906173  7.907971  7.908341
#> [2626]  7.909946  7.911943  7.912526  7.913344  7.914179  7.914503  7.916771
#> [2633]  7.917069  7.917911  7.919806  7.919848  7.921838  7.924387  7.924847
#> [2640]  7.925061  7.925114  7.928497  7.928571  7.928834  7.929562  7.929727
#> [2647]  7.931059  7.935187  7.935824  7.943101  7.943197  7.944780  7.946403
#> [2654]  7.948374  7.949428  7.950575  7.950673  7.950994  7.951116  7.953692
#> [2661]  7.958392  7.959128  7.960160  7.960356  7.962697  7.963585  7.964639
#> [2668]  7.964908  7.964984  7.965779  7.967156  7.967198  7.967222  7.967394
#> [2675]  7.967589  7.969022  7.970819  7.971932  7.971996  7.972910  7.973016
#> [2682]  7.974022  7.974299  7.974503  7.975235  7.975724  7.976318  7.978133
#> [2689]  7.979603  7.980496  7.982575  7.982598  7.984348  7.984701  7.990604
#> [2696]  7.993543  7.996173  7.996371  7.997274  7.998066  7.999105  8.000102
#> [2703]  8.001750  8.002279  8.002523  8.002563  8.002586  8.004266  8.006923
#> [2710]  8.007004  8.007347  8.008758  8.009411  8.009894  8.011502  8.012779
#> [2717]  8.014083  8.015398  8.015884  8.017832  8.018560  8.020524  8.022909
#> [2724]  8.023541  8.025651  8.027966  8.028270  8.029054  8.029698  8.029895
#> [2731]  8.030797  8.030875  8.032524  8.038013  8.040643  8.042372  8.044598
#> [2738]  8.044688  8.044912  8.046381  8.047493  8.048321  8.050595  8.050702
#> [2745]  8.050768  8.051574  8.055831  8.057011  8.057018  8.057212  8.063402
#> [2752]  8.064034  8.064459  8.067689  8.068246  8.068543  8.069837  8.072324
#> [2759]  8.072360  8.072729  8.073360  8.074199  8.074509  8.077607  8.078183
#> [2766]  8.079284  8.082689  8.083272  8.083625  8.085719  8.088031  8.089496
#> [2773]  8.090638  8.091825  8.093483  8.093778  8.093785  8.095422  8.098609
#> [2780]  8.098899  8.099355  8.099477  8.100974  8.104181  8.105818  8.105996
#> [2787]  8.106722  8.107240  8.110570  8.110817  8.111020  8.114458  8.120330
#> [2794]  8.120928  8.121098  8.123026  8.124399  8.124416  8.126411  8.126703
#> [2801]  8.130208  8.133003  8.133056  8.133527  8.137331  8.140496  8.140635
#> [2808]  8.141830  8.142487  8.145790  8.146561  8.147103  8.147568  8.147606
#> [2815]  8.150360  8.150865  8.152188  8.153179  8.153690  8.154858  8.159229
#> [2822]  8.159308  8.160054  8.161155  8.161498  8.161776  8.162556  8.166517
#> [2829]  8.171709  8.172030  8.172938  8.173642  8.174510  8.175324  8.176604
#> [2836]  8.177016  8.183799  8.185786  8.185806  8.187812  8.188416  8.188765
#> [2843]  8.189450  8.190721  8.190741  8.191816  8.194783  8.195033  8.195710
#> [2850]  8.197676  8.202381  8.204429  8.205159  8.205566  8.210040  8.210611
#> [2857]  8.213175  8.214535  8.218677  8.221542  8.221665  8.221867  8.222484
#> [2864]  8.223110  8.224998  8.225325  8.225906  8.226719  8.228041  8.230984
#> [2871]  8.232050  8.232428  8.232758  8.234012  8.236784  8.238565  8.238656
#> [2878]  8.241973  8.242150  8.242234  8.243271  8.243284  8.245447  8.246119
#> [2885]  8.248662  8.249664  8.251537  8.252090  8.252899  8.254059  8.254186
#> [2892]  8.254647  8.255741  8.256822  8.257079  8.257611  8.258585  8.258834
#> [2899]  8.259214  8.260076  8.260663  8.261818  8.261850  8.267563  8.268919
#> [2906]  8.269330  8.270511  8.271003  8.274362  8.280541  8.281507  8.281710
#> [2913]  8.281810  8.283615  8.284982  8.285394  8.285620  8.285741  8.286148
#> [2920]  8.288329  8.288898  8.289036  8.292157  8.292860  8.293491  8.294123
#> [2927]  8.294247  8.294591  8.296081  8.296252  8.297475  8.299062  8.304754
#> [2934]  8.304968  8.305114  8.305691  8.305862  8.307533  8.307881  8.308118
#> [2941]  8.310585  8.311560  8.314317  8.314411  8.314615  8.319256  8.319457
#> [2948]  8.320103  8.321937  8.322905  8.323560  8.326452  8.327138  8.329397
#> [2955]  8.330572  8.331233  8.331692  8.337152  8.337781  8.341127  8.342693
#> [2962]  8.343873  8.344110  8.345335  8.346034  8.346115  8.346446  8.346533
#> [2969]  8.349885  8.350065  8.351563  8.352119  8.352316  8.353304  8.354777
#> [2976]  8.355203  8.355917  8.356819  8.358703  8.359806  8.361533  8.361629
#> [2983]  8.362788  8.363806  8.364617  8.364675  8.365164  8.365392  8.366189
#> [2990]  8.366414  8.367794  8.368291  8.369813  8.370391  8.370911  8.372180
#> [2997]  8.374438  8.376086  8.376301  8.377015  8.377427  8.378017  8.378783
#> [3004]  8.378854  8.380606  8.381209  8.382110  8.382167  8.383877  8.384764
#> [3011]  8.384915  8.388038  8.388526  8.390704  8.391039  8.392230  8.392392
#> [3018]  8.395524  8.395597  8.396173  8.397603  8.398216  8.399201  8.399441
#> [3025]  8.403799  8.404182  8.404920  8.405061  8.407258  8.408539  8.408688
#> [3032]  8.410041  8.410441  8.410452  8.410931  8.411356  8.411515  8.412618
#> [3039]  8.412789  8.413189  8.414980  8.415889  8.417470  8.418347  8.419716
#> [3046]  8.420388  8.420903  8.421302  8.430020  8.430643  8.431232  8.432108
#> [3053]  8.433215  8.434888  8.438797  8.439159  8.439363  8.442936  8.444227
#> [3060]  8.444599  8.447272  8.449052  8.449253  8.450608  8.451080  8.451551
#> [3067]  8.453401  8.456088  8.456437  8.456511  8.458529  8.460417  8.461071
#> [3074]  8.462285  8.464230  8.464565  8.465294  8.466411  8.469333  8.469405
#> [3081]  8.469456  8.469630  8.472007  8.474013  8.475671  8.476403  8.477404
#> [3088]  8.478379  8.480103  8.480529  8.483438  8.483448  8.483647  8.483670
#> [3095]  8.483974  8.484448  8.486424  8.487793  8.488842  8.488956  8.489456
#> [3102]  8.491357  8.495149  8.495619  8.497995  8.499302  8.499553  8.499639
#> [3109]  8.502697  8.502994  8.503925  8.505549  8.505596  8.505996  8.508183
#> [3116]  8.508987  8.509635  8.511996  8.513549  8.514753  8.515163  8.515381
#> [3123]  8.515526  8.518752  8.518835  8.519120  8.521195  8.524338  8.525565
#> [3130]  8.526967  8.527424  8.528124  8.531231  8.535659  8.535794  8.536881
#> [3137]  8.536885  8.537584  8.537882  8.538587  8.540247  8.540838  8.541565
#> [3144]  8.542861  8.546278  8.547034  8.547081  8.547582  8.548621  8.549350
#> [3151]  8.549648  8.551092  8.552877  8.553488  8.554158  8.554471  8.556209
#> [3158]  8.557786  8.557798  8.557925  8.558011  8.558484  8.558559  8.560059
#> [3165]  8.560261  8.561483  8.562394  8.562704  8.565174  8.565563  8.565628
#> [3172]  8.566706  8.568807  8.570381  8.571021  8.571812  8.571832  8.572584
#> [3179]  8.573092  8.573195  8.573245  8.573493  8.574331  8.574591  8.577601
#> [3186]  8.577668  8.577685  8.577855  8.579986  8.580185  8.580251  8.580463
#> [3193]  8.586314  8.586909  8.593003  8.594664  8.595466  8.597682  8.600319
#> [3200]  8.600479  8.600621  8.600654  8.604135  8.605011  8.607607  8.607787
#> [3207]  8.608993  8.609291  8.614759  8.615620  8.615763  8.616705  8.617068
#> [3214]  8.619193  8.623103  8.623855  8.626360  8.627023  8.628262  8.629912
#> [3221]  8.630966  8.631219  8.634299  8.634369  8.635591  8.638182  8.638473
#> [3228]  8.638536  8.640290  8.640452  8.641385  8.642428  8.642507  8.642706
#> [3235]  8.643069  8.644537  8.646092  8.646370  8.646495  8.648554  8.649496
#> [3242]  8.649607  8.649841  8.649850  8.650858  8.651747  8.652761  8.654762
#> [3249]  8.655367  8.657417  8.657631  8.659405  8.663915  8.664308  8.664515
#> [3256]  8.665373  8.666549  8.667892  8.667984  8.671368  8.672399  8.672994
#> [3263]  8.673543  8.674977  8.675913  8.677326  8.677934  8.681280  8.684010
#> [3270]  8.685577  8.686873  8.688035  8.689430  8.695054  8.695958  8.696343
#> [3277]  8.697722  8.698999  8.700097  8.700663  8.702019  8.703054  8.703185
#> [3284]  8.704809  8.706725  8.707572  8.708525  8.708951  8.714177  8.714239
#> [3291]  8.714867  8.716150  8.716197  8.717727  8.717736  8.718221  8.718589
#> [3298]  8.719203  8.722772  8.724778  8.725430  8.726674  8.727055  8.727135
#> [3305]  8.731137  8.732133  8.733331  8.734715  8.735905  8.737137  8.737482
#> [3312]  8.739629  8.740163  8.741869  8.745603  8.745843  8.749573  8.752365
#> [3319]  8.752500  8.753787  8.755337  8.756931  8.758390  8.759222  8.761163
#> [3326]  8.761188  8.762260  8.762437  8.762726  8.763348  8.764490  8.764552
#> [3333]  8.765525  8.765730  8.766465  8.767593  8.767790  8.770180  8.770244
#> [3340]  8.770598  8.774326  8.775925  8.776569  8.778973  8.780801  8.782419
#> [3347]  8.786079  8.787333  8.791696  8.793309  8.793429  8.794463  8.795028
#> [3354]  8.795984  8.797624  8.799233  8.799809  8.802672  8.803777  8.804106
#> [3361]  8.805103  8.806032  8.807529  8.810031  8.810233  8.810242  8.811069
#> [3368]  8.813006  8.814423  8.815255  8.815758  8.816518  8.817205  8.819498
#> [3375]  8.820216  8.820598  8.820713  8.820746  8.821109  8.821375  8.825062
#> [3382]  8.825303  8.825680  8.832249  8.833171  8.834022  8.836274  8.836685
#> [3389]  8.836978  8.841646  8.843636  8.844065  8.844150  8.847317  8.849496
#> [3396]  8.850010  8.850222  8.851757  8.852444  8.852847  8.854262  8.856271
#> [3403]  8.856955  8.861581  8.862421  8.863112  8.863855  8.864636  8.867690
#> [3410]  8.869260  8.870457  8.871408  8.872979  8.873438  8.875371  8.877155
#> [3417]  8.878340  8.878737  8.879731  8.882172  8.882663  8.883668  8.884798
#> [3424]  8.886105  8.887280  8.888318  8.888648  8.888941  8.893834  8.896265
#> [3431]  8.898281  8.900388  8.900916  8.904182  8.908547  8.915683  8.919841
#> [3438]  8.923474  8.923844  8.924533  8.924940  8.926548  8.930034  8.931958
#> [3445]  8.937552  8.941860  8.943352  8.943396  8.946001  8.946683  8.947462
#> [3452]  8.949589  8.951629  8.952262  8.952335  8.953249  8.955437  8.955797
#> [3459]  8.957368  8.957949  8.958302  8.961175  8.970080  8.970108  8.970276
#> [3466]  8.974415  8.983986  8.984545  8.985750  8.988224  8.989206  8.989219
#> [3473]  8.991785  8.992736  8.993147  8.995433  9.002659  9.003054  9.003636
#> [3480]  9.006449  9.008897  9.008902  9.009903  9.011104  9.015312  9.015520
#> [3487]  9.016158  9.019929  9.020110  9.022283  9.022895  9.023195  9.027230
#> [3494]  9.031576  9.031730  9.032474  9.035081  9.035740  9.036773  9.037017
#> [3501]  9.037304  9.042693  9.044150  9.046509  9.047737  9.050493  9.050832
#> [3508]  9.051192  9.053302  9.053913  9.054575  9.057451  9.061361  9.064714
#> [3515]  9.065148  9.066041  9.068375  9.074405  9.075669  9.076006  9.076143
#> [3522]  9.078214  9.079503  9.080767  9.081851  9.084375  9.084590  9.085076
#> [3529]  9.085630  9.085901  9.086042  9.088595  9.088646  9.089769  9.090782
#> [3536]  9.090981  9.091303  9.099982  9.101872  9.103041  9.103619  9.106312
#> [3543]  9.107413  9.107943  9.109522  9.109584  9.111964  9.112204  9.114620
#> [3550]  9.120171  9.121716  9.122027  9.123374  9.125955  9.132075  9.134542
#> [3557]  9.135826  9.140252  9.140708  9.141059  9.141786  9.143407  9.144639
#> [3564]  9.145149  9.145925  9.147977  9.149036  9.149815  9.152521  9.155406
#> [3571]  9.155503  9.155930  9.156509  9.157353  9.158127  9.158651  9.159788
#> [3578]  9.174155  9.174698  9.175742  9.175914  9.175973  9.180946  9.181850
#> [3585]  9.184785  9.185663  9.186573  9.186587  9.186761  9.186794  9.188197
#> [3592]  9.188405  9.188822  9.190471  9.192968  9.193743  9.195802  9.204529
#> [3599]  9.204860  9.206427  9.209966  9.212557  9.213688  9.214400  9.215439
#> [3606]  9.215881  9.216503  9.217755  9.218139  9.219222  9.219829  9.221271
#> [3613]  9.221621  9.222446  9.222762  9.225454  9.226756  9.227178  9.228148
#> [3620]  9.228869  9.230845  9.230868  9.233018  9.236073  9.238282  9.240927
#> [3627]  9.243637  9.244637  9.246072  9.253709  9.254371  9.254581  9.258080
#> [3634]  9.260508  9.261150  9.261999  9.263007  9.267789  9.268144  9.269299
#> [3641]  9.271805  9.272659  9.275337  9.281812  9.285040  9.285257  9.287395
#> [3648]  9.287865  9.289848  9.291128  9.291516  9.292653  9.293878  9.294325
#> [3655]  9.295309  9.295713  9.296655  9.297656  9.298053  9.298813  9.300018
#> [3662]  9.300610  9.302747  9.303820  9.304673  9.309464  9.310791  9.312245
#> [3669]  9.312581  9.312588  9.313103  9.314107  9.314405  9.314589  9.314957
#> [3676]  9.317256  9.320029  9.320766  9.321198  9.321818  9.321856  9.322144
#> [3683]  9.322265  9.323903  9.324477  9.325622  9.325926  9.326371  9.327389
#> [3690]  9.330845  9.331785  9.331895  9.333492  9.334570  9.334822  9.336140
#> [3697]  9.341289  9.342343  9.343300  9.343734  9.348817  9.349110  9.352292
#> [3704]  9.353996  9.354336  9.357516  9.361277  9.361361  9.362671  9.363259
#> [3711]  9.364064  9.364735  9.365678  9.366556  9.370540  9.371928  9.372025
#> [3718]  9.373396  9.373613  9.376577  9.378428  9.380409  9.381142  9.381234
#> [3725]  9.384668  9.387942  9.388886  9.389450  9.390229  9.390540  9.391662
#> [3732]  9.392277  9.392742  9.396761  9.397202  9.397323  9.397668  9.400325
#> [3739]  9.406802  9.408146  9.408915  9.409259  9.410775  9.412158  9.414436
#> [3746]  9.414821  9.420492  9.420755  9.421826  9.424519  9.426052  9.426695
#> [3753]  9.428575  9.429291  9.429373  9.429374  9.430623  9.430789  9.430890
#> [3760]  9.432203  9.432651  9.434996  9.436371  9.436928  9.439803  9.440049
#> [3767]  9.440255  9.441321  9.445570  9.446372  9.446651  9.447448  9.447465
#> [3774]  9.449339  9.449573  9.449597  9.453425  9.453513  9.454971  9.458894
#> [3781]  9.458982  9.459810  9.461972  9.464336  9.464805  9.466562  9.466937
#> [3788]  9.469168  9.470208  9.470410  9.471623  9.472435  9.473734  9.476060
#> [3795]  9.477849  9.478234  9.483317  9.483912  9.484836  9.485093  9.485433
#> [3802]  9.485572  9.486332  9.487009  9.487904  9.488974  9.489057  9.489830
#> [3809]  9.491934  9.492541  9.492634  9.495400  9.495491  9.497733  9.499843
#> [3816]  9.500535  9.501174  9.506811  9.507161  9.508806  9.510406  9.512897
#> [3823]  9.513667  9.514616  9.516033  9.516244  9.516480  9.517482  9.518332
#> [3830]  9.518514  9.519121  9.520094  9.520456  9.521218  9.523923  9.525528
#> [3837]  9.534346  9.534600  9.535020  9.537259  9.541345  9.542546  9.544813
#> [3844]  9.546763  9.548075  9.549080  9.552191  9.555348  9.557330  9.558069
#> [3851]  9.560925  9.561150  9.562563  9.565394  9.568940  9.569484  9.570250
#> [3858]  9.570420  9.571342  9.571555  9.571584  9.573573  9.574801  9.575229
#> [3865]  9.577259  9.577335  9.581586  9.581684  9.582244  9.585314  9.586953
#> [3872]  9.587542  9.587848  9.589407  9.590288  9.591366  9.593398  9.595110
#> [3879]  9.595595  9.595685  9.599107  9.600303  9.608915  9.609321  9.610641
#> [3886]  9.610709  9.614140  9.616552  9.618359  9.620054  9.620913  9.621161
#> [3893]  9.623375  9.624184  9.626365  9.634049  9.634159  9.634234  9.638376
#> [3900]  9.638395  9.639133  9.640636  9.641518  9.642906  9.645571  9.646513
#> [3907]  9.649587  9.649732  9.652829  9.652845  9.654143  9.654495  9.655809
#> [3914]  9.656032  9.658705  9.659885  9.660509  9.660511  9.662146  9.665183
#> [3921]  9.665271  9.668573  9.669084  9.670286  9.670806  9.671098  9.673256
#> [3928]  9.675572  9.675952  9.677698  9.683466  9.685297  9.685448  9.686310
#> [3935]  9.686718  9.688027  9.689472  9.696702  9.696838  9.696983  9.699138
#> [3942]  9.700247  9.700509  9.700569  9.703647  9.704324  9.704439  9.707522
#> [3949]  9.709853  9.717100  9.717584  9.718397  9.719034  9.721194  9.721953
#> [3956]  9.722471  9.722669  9.722774  9.724518  9.728739  9.729734  9.732728
#> [3963]  9.737673  9.739158  9.740126  9.740624  9.741477  9.741595  9.743265
#> [3970]  9.747558  9.750839  9.752240  9.757845  9.761180  9.761705  9.763720
#> [3977]  9.763847  9.764686  9.764725  9.765398  9.766316  9.766450  9.771312
#> [3984]  9.771720  9.773106  9.776433  9.781286  9.782635  9.783106  9.786348
#> [3991]  9.786732  9.788369  9.789234  9.790020  9.790611  9.793480  9.794162
#> [3998]  9.794205  9.794700  9.794727  9.795160  9.796989  9.797282  9.797609
#> [4005]  9.798715  9.801757  9.804817  9.808595  9.810494  9.818309  9.819101
#> [4012]  9.821873  9.823581  9.825989  9.829298  9.831014  9.832290  9.833455
#> [4019]  9.834392  9.837582  9.841691  9.842949  9.843483  9.844245  9.844813
#> [4026]  9.846940  9.847433  9.854821  9.857773  9.859233  9.859487  9.861457
#> [4033]  9.861605  9.862718  9.864258  9.864814  9.865488  9.865756  9.867142
#> [4040]  9.867281  9.867863  9.869772  9.876686  9.876875  9.879065  9.879252
#> [4047]  9.884195  9.884931  9.885292  9.886390  9.886517  9.886554  9.887498
#> [4054]  9.889626  9.892015  9.892021  9.893091  9.895845  9.895959  9.896076
#> [4061]  9.896126  9.896489  9.896550  9.896766  9.897440  9.900939  9.902861
#> [4068]  9.903911  9.905088  9.905409  9.908163  9.909160  9.910565  9.913359
#> [4075]  9.914466  9.914964  9.915522  9.917984  9.920934  9.922470  9.925470
#> [4082]  9.927353  9.927414  9.929131  9.930129  9.934324  9.934526  9.935728
#> [4089]  9.936634  9.937818  9.939009  9.940074  9.941928  9.942549  9.944577
#> [4096]  9.945062  9.945080  9.945274  9.945781  9.947245  9.949459  9.952128
#> [4103]  9.952464  9.955527  9.955993  9.956648  9.957646  9.958233  9.961530
#> [4110]  9.962317  9.965240  9.966780  9.966856  9.968311  9.973190  9.977606
#> [4117]  9.980170  9.980726  9.983060  9.984908  9.986207  9.986622  9.987680
#> [4124]  9.989137  9.992896  9.996611  9.998073 10.000884 10.004164 10.004634
#> [4131] 10.007823 10.008387 10.008943 10.013104 10.015731 10.016402 10.018278
#> [4138] 10.019494 10.019503 10.019725 10.022882 10.024502 10.026482 10.026629
#> [4145] 10.027764 10.031237 10.031846 10.038397 10.040598 10.041132 10.041446
#> [4152] 10.042059 10.045100 10.048084 10.049684 10.050547 10.050604 10.054014
#> [4159] 10.054790 10.056502 10.063867 10.066082 10.066538 10.069066 10.069575
#> [4166] 10.070766 10.075620 10.079324 10.079445 10.080117 10.080604 10.082525
#> [4173] 10.088502 10.089334 10.090379 10.090905 10.091077 10.091600 10.095070
#> [4180] 10.096798 10.100674 10.100960 10.106545 10.107400 10.107513 10.108085
#> [4187] 10.109763 10.109853 10.111141 10.111290 10.111368 10.112595 10.116553
#> [4194] 10.117074 10.120724 10.124675 10.127163 10.127291 10.131782 10.132193
#> [4201] 10.132786 10.132869 10.134803 10.135584 10.136912 10.141553 10.143504
#> [4208] 10.144404 10.144456 10.146208 10.146712 10.146845 10.148902 10.150412
#> [4215] 10.155713 10.155958 10.157291 10.158934 10.162107 10.162285 10.162531
#> [4222] 10.162564 10.162889 10.164653 10.165365 10.166084 10.171475 10.173028
#> [4229] 10.176299 10.176362 10.176995 10.180530 10.184593 10.184951 10.185708
#> [4236] 10.187192 10.187979 10.190323 10.192176 10.192908 10.192959 10.196476
#> [4243] 10.197806 10.198232 10.201398 10.203462 10.203669 10.204453 10.204796
#> [4250] 10.207113 10.207936 10.208762 10.210085 10.211436 10.214481 10.219401
#> [4257] 10.220739 10.221674 10.221765 10.222109 10.222197 10.222318 10.223024
#> [4264] 10.224526 10.225542 10.228179 10.230273 10.230823 10.232226 10.232907
#> [4271] 10.233098 10.235690 10.237528 10.237925 10.240612 10.241694 10.242800
#> [4278] 10.244584 10.244884 10.249220 10.249772 10.249937 10.250309 10.251054
#> [4285] 10.251522 10.255726 10.260333 10.263361 10.269291 10.269975 10.270951
#> [4292] 10.276895 10.279789 10.280784 10.281672 10.283641 10.283909 10.284127
#> [4299] 10.284751 10.285105 10.287056 10.291751 10.293007 10.293812 10.298558
#> [4306] 10.303768 10.307935 10.312362 10.314914 10.317203 10.320898 10.324576
#> [4313] 10.325476 10.327385 10.330246 10.330262 10.332120 10.333343 10.336858
#> [4320] 10.341417 10.341590 10.344363 10.347846 10.355420 10.356025 10.357556
#> [4327] 10.362456 10.364045 10.367549 10.368136 10.369482 10.371357 10.374423
#> [4334] 10.375222 10.376242 10.378141 10.381923 10.382724 10.383153 10.384083
#> [4341] 10.384352 10.386378 10.391211 10.394937 10.395672 10.397083 10.397728
#> [4348] 10.399558 10.402394 10.403258 10.404789 10.405144 10.406025 10.407916
#> [4355] 10.410774 10.414729 10.414924 10.415185 10.415318 10.416874 10.417976
#> [4362] 10.419501 10.425726 10.427290 10.427660 10.428629 10.431103 10.432289
#> [4369] 10.433672 10.436330 10.437326 10.438353 10.444884 10.447140 10.447272
#> [4376] 10.448525 10.449892 10.455451 10.458696 10.459783 10.462910 10.468541
#> [4383] 10.469876 10.470240 10.475037 10.477249 10.477452 10.478034 10.478317
#> [4390] 10.481385 10.484525 10.491484 10.492459 10.493018 10.493376 10.498064
#> [4397] 10.500983 10.501044 10.501285 10.505555 10.507180 10.516235 10.517132
#> [4404] 10.518353 10.523631 10.523979 10.524375 10.527955 10.531645 10.534872
#> [4411] 10.535054 10.544044 10.545746 10.548092 10.548634 10.551585 10.552932
#> [4418] 10.553082 10.554587 10.556869 10.560018 10.560960 10.562467 10.563401
#> [4425] 10.569298 10.571302 10.572470 10.572569 10.572768 10.574042 10.574522
#> [4432] 10.574636 10.575741 10.575993 10.580610 10.585398 10.585657 10.588714
#> [4439] 10.592012 10.593111 10.595003 10.595607 10.598058 10.599102 10.599524
#> [4446] 10.601823 10.601984 10.603215 10.604018 10.604196 10.606533 10.608438
#> [4453] 10.609010 10.610491 10.614700 10.615684 10.630307 10.631637 10.632163
#> [4460] 10.638278 10.638715 10.638790 10.641503 10.642095 10.642235 10.643572
#> [4467] 10.644370 10.644574 10.644775 10.646449 10.649705 10.650332 10.654778
#> [4474] 10.661154 10.661312 10.668591 10.673036 10.673409 10.681741 10.684090
#> [4481] 10.685256 10.685910 10.687804 10.688441 10.692770 10.693170 10.693647
#> [4488] 10.694030 10.695664 10.696020 10.698758 10.700633 10.702145 10.712433
#> [4495] 10.713218 10.713655 10.714032 10.717596 10.724695 10.726557 10.727254
#> [4502] 10.728913 10.730083 10.731027 10.733447 10.733471 10.734225 10.737952
#> [4509] 10.741223 10.741268 10.743106 10.751672 10.757872 10.762733 10.765391
#> [4516] 10.766567 10.766680 10.767409 10.767476 10.768797 10.771638 10.776333
#> [4523] 10.776910 10.791785 10.793564 10.797483 10.797649 10.800085 10.803558
#> [4530] 10.804445 10.804838 10.808039 10.808744 10.812165 10.813331 10.813851
#> [4537] 10.814717 10.816605 10.821305 10.823322 10.825681 10.825773 10.828262
#> [4544] 10.830279 10.832476 10.840337 10.840340 10.840540 10.852360 10.853332
#> [4551] 10.857976 10.860118 10.860954 10.863007 10.866012 10.874330 10.879452
#> [4558] 10.880245 10.887583 10.889606 10.891984 10.897419 10.897672 10.899984
#> [4565] 10.900763 10.905584 10.907401 10.909003 10.911645 10.914645 10.920928
#> [4572] 10.925485 10.926159 10.928881 10.932188 10.932479 10.937636 10.938301
#> [4579] 10.948056 10.952561 10.955747 10.960300 10.960536 10.962454 10.962580
#> [4586] 10.963077 10.964117 10.969500 10.970873 10.972402 10.975648 10.978596
#> [4593] 10.985464 10.985508 10.988169 10.988304 10.992957 10.993006 11.000821
#> [4600] 11.001841 11.003264 11.003503 11.004303 11.009755 11.014622 11.023400
#> [4607] 11.024375 11.024554 11.030463 11.033112 11.035707 11.044663 11.049632
#> [4614] 11.053596 11.054015 11.054089 11.054861 11.057728 11.058018 11.058818
#> [4621] 11.060698 11.065443 11.068971 11.071009 11.072483 11.072818 11.075401
#> [4628] 11.082383 11.084847 11.087050 11.094761 11.095546 11.095667 11.098824
#> [4635] 11.099283 11.099964 11.100976 11.102845 11.103455 11.106501 11.106831
#> [4642] 11.106851 11.107601 11.109487 11.111734 11.115263 11.118707 11.121720
#> [4649] 11.122407 11.122735 11.128384 11.131557 11.132546 11.135017 11.135332
#> [4656] 11.136177 11.141350 11.142155 11.143966 11.146961 11.148066 11.152397
#> [4663] 11.153658 11.157528 11.158065 11.161764 11.164834 11.167194 11.173251
#> [4670] 11.175174 11.179371 11.180095 11.181399 11.182991 11.183554 11.189275
#> [4677] 11.193884 11.194092 11.195626 11.197423 11.198279 11.199424 11.202019
#> [4684] 11.204588 11.207981 11.210251 11.210371 11.213055 11.213740 11.214478
#> [4691] 11.215437 11.220444 11.224201 11.229991 11.232291 11.233463 11.234189
#> [4698] 11.235060 11.236578 11.237798 11.244899 11.245332 11.248048 11.249513
#> [4705] 11.249650 11.250606 11.252140 11.252930 11.254307 11.256234 11.258154
#> [4712] 11.259252 11.259276 11.259392 11.260206 11.261225 11.262112 11.263768
#> [4719] 11.265245 11.265965 11.267890 11.268455 11.269374 11.271658 11.271705
#> [4726] 11.273073 11.274257 11.274345 11.282481 11.288624 11.290663 11.291686
#> [4733] 11.292637 11.293501 11.296100 11.306308 11.313883 11.315218 11.317123
#> [4740] 11.326229 11.326252 11.326495 11.330247 11.332265 11.333135 11.333432
#> [4747] 11.333598 11.335816 11.337512 11.337919 11.338055 11.340523 11.344938
#> [4754] 11.346257 11.347432 11.350838 11.351737 11.353317 11.354236 11.355357
#> [4761] 11.356198 11.357309 11.361382 11.364164 11.368430 11.371124 11.372453
#> [4768] 11.376129 11.377528 11.378825 11.382727 11.386310 11.386466 11.386903
#> [4775] 11.387116 11.388904 11.394540 11.395463 11.396353 11.398600 11.399192
#> [4782] 11.402169 11.402412 11.403226 11.404895 11.405120 11.407780 11.408732
#> [4789] 11.409533 11.410065 11.416271 11.417853 11.420374 11.422534 11.426762
#> [4796] 11.429990 11.431338 11.432672 11.434715 11.440788 11.443983 11.444796
#> [4803] 11.446752 11.449387 11.449864 11.449880 11.454593 11.460433 11.461945
#> [4810] 11.462980 11.464821 11.478187 11.478544 11.478888 11.489012 11.490258
#> [4817] 11.495273 11.502033 11.502631 11.503940 11.505592 11.506812 11.512732
#> [4824] 11.521508 11.523560 11.531601 11.535678 11.536750 11.540296 11.540586
#> [4831] 11.543325 11.543781 11.546915 11.549051 11.552789 11.558649 11.566802
#> [4838] 11.567034 11.567344 11.568933 11.575628 11.576656 11.580205 11.580622
#> [4845] 11.581053 11.595122 11.597205 11.605111 11.613353 11.613586 11.617135
#> [4852] 11.618361 11.618777 11.619224 11.620507 11.620684 11.625745 11.636571
#> [4859] 11.636632 11.637819 11.639811 11.640743 11.642030 11.644287 11.649058
#> [4866] 11.650634 11.650885 11.653271 11.655908 11.656478 11.656703 11.657229
#> [4873] 11.657292 11.658640 11.658700 11.662408 11.663364 11.666234 11.668382
#> [4880] 11.668818 11.669530 11.673042 11.673114 11.675516 11.676508 11.680729
#> [4887] 11.681915 11.696152 11.698924 11.703277 11.704386 11.712358 11.717466
#> [4894] 11.718600 11.721511 11.723937 11.731437 11.735747 11.736142 11.749755
#> [4901] 11.763708 11.763778 11.772307 11.779107 11.780986 11.787617 11.788605
#> [4908] 11.792700 11.793968 11.795504 11.796928 11.802907 11.807211 11.811595
#> [4915] 11.813759 11.821091 11.823886 11.824989 11.835796 11.835805 11.837457
#> [4922] 11.841592 11.851130 11.852639 11.854108 11.854585 11.859100 11.864000
#> [4929] 11.867746 11.877533 11.884597 11.885114 11.886833 11.889094 11.891431
#> [4936] 11.895489 11.902273 11.903858 11.905648 11.910699 11.920040 11.920274
#> [4943] 11.921295 11.924813 11.925464 11.926249 11.942631 11.952506 11.959204
#> [4950] 11.963607 11.964684 11.967591 11.970569 11.971724 11.981849 11.987708
#> [4957] 11.996250 11.998521 11.998673 12.000562 12.007095 12.008240 12.008948
#> [4964] 12.015084 12.020168 12.023551 12.024463 12.026011 12.032219 12.046767
#> [4971] 12.062090 12.063196 12.084123 12.088924 12.090712 12.097666 12.105407
#> [4978] 12.105812 12.106752 12.107408 12.112645 12.116316 12.120333 12.124373
#> [4985] 12.128035 12.129417 12.130550 12.136857 12.137435 12.156677 12.160788
#> [4992] 12.167520 12.168157 12.171006 12.180026 12.183592 12.187182 12.189354
#> [4999] 12.202196 12.213360 12.223888 12.225296 12.228537 12.228606 12.236943
#> [5006] 12.247168 12.247737 12.272190 12.272376 12.276988 12.285025 12.291347
#> [5013] 12.293458 12.293655 12.309013 12.334602 12.341750 12.352574 12.356021
#> [5020] 12.393153 12.393390 12.402313 12.403783 12.404562 12.405113 12.405795
#> [5027] 12.414676 12.433099 12.437083 12.440618 12.442219 12.453473 12.462773
#> [5034] 12.469288 12.472445 12.481818 12.489611 12.495034 12.497743 12.498739
#> [5041] 12.508491 12.508747 12.510235 12.511340 12.515303 12.520220 12.526220
#> [5048] 12.534910 12.539402 12.541175 12.541945 12.543384 12.561268 12.562384
#> [5055] 12.578259 12.593170 12.613505 12.633984 12.646982 12.647404 12.652614
#> [5062] 12.659668 12.661784 12.665864 12.669630 12.676252 12.679478 12.681168
#> [5069] 12.684195 12.693420 12.732826 12.767965 12.774912 12.788354 12.795722
#> [5076] 12.827656 12.834318 12.834416 12.863790 12.867119 12.884097 12.886725
#> [5083] 12.889825 12.923374 12.930357 12.938205 12.998100 13.002538 13.002655
#> [5090] 13.003397 13.032034 13.032538 13.037352 13.049455 13.060079 13.068273
#> [5097] 13.121465 13.130443 13.159924 13.165906 13.167057 13.186829 13.200693
#> [5104] 13.201307 13.203640 13.274295 13.283580 13.393983 13.432739 13.454298
#> [5111] 13.467261 13.487412 13.671700 13.764203 13.766897 13.769740 13.779365
#> [5118] 13.799265 13.806023 13.818302 13.879100 13.919596 13.939101 14.010936
#> [5125] 14.059320 14.067351 14.082262 14.128692 14.173357 14.236301 14.248875
#> [5132] 14.269512
dat <- dat %>% mutate(temp = ifelse(quarter == 1, NA, temp))
#> mutate: changed 5,144 values (60%) of 'temp' (5144 new NA)
sort(unique(filter(dat, quarter == 1)$temp))
#> filter: removed 3,452 rows (40%), 5,144 rows remaining
#> numeric(0)

Add back in the data that get environmental variables added

fishdat2 <- fishdat %>% filter(!year %in% unique(dat$year))
#> filter: removed 8,597 rows (96%), 342 rows remaining
sort(unique(fishdat2$year))
#> [1] 1992 2020
sort(unique(dat$year))
#>  [1] 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007
#> [16] 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019

dat <- bind_rows(dat, fishdat2)

sort(unique(dat$year))
#>  [1] 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006
#> [16] 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020

dat %>% group_by(year, quarter) %>% summarise(mean_temp = mean(temp)) %>% as.data.frame()
#> group_by: 2 grouping variables (year, quarter)
#> summarise: now 58 rows and 3 columns, one group variable remaining (year)
#>    year quarter mean_temp
#> 1  1992       1        NA
#> 2  1992       4        NA
#> 3  1993       1        NA
#> 4  1993       4  7.246987
#> 5  1994       1        NA
#> 6  1994       4  8.091562
#> 7  1995       1        NA
#> 8  1995       4  8.407659
#> 9  1996       1        NA
#> 10 1996       4  8.299413
#> 11 1997       1        NA
#> 12 1997       4  8.528001
#> 13 1998       1        NA
#> 14 1998       4  7.360535
#> 15 1999       1        NA
#> 16 1999       4  8.453743
#> 17 2000       1        NA
#> 18 2000       4  8.082708
#> 19 2001       1        NA
#> 20 2001       4  8.077808
#> 21 2002       1        NA
#> 22 2002       4  8.748811
#> 23 2003       1        NA
#> 24 2003       4  8.485861
#> 25 2004       1        NA
#> 26 2004       4  8.506533
#> 27 2005       1        NA
#> 28 2005       4  7.659136
#> 29 2006       1        NA
#> 30 2006       4  8.112038
#> 31 2007       1        NA
#> 32 2007       4  8.120095
#> 33 2008       1        NA
#> 34 2008       4  8.407919
#> 35 2009       1        NA
#> 36 2009       4  8.943984
#> 37 2010       1        NA
#> 38 2010       4  6.407201
#> 39 2011       1        NA
#> 40 2011       4  7.330912
#> 41 2012       1        NA
#> 42 2012       4  7.976262
#> 43 2013       1        NA
#> 44 2013       4  7.473096
#> 45 2014       1        NA
#> 46 2014       4  9.228403
#> 47 2015       1        NA
#> 48 2015       4  8.406676
#> 49 2016       1        NA
#> 50 2016       4  9.294175
#> 51 2017       1        NA
#> 52 2017       4  8.372061
#> 53 2018       1        NA
#> 54 2018       4  9.580076
#> 55 2019       1        NA
#> 56 2019       4  8.319438
#> 57 2020       1        NA
#> 58 2020       4        NA

Add UTM coords

# First add UTM coords
# Add UTM coords
# Function
LongLatToUTM <- function(x, y, zone){
  xy <- data.frame(ID = 1:length(x), X = x, Y = y)
  coordinates(xy) <- c("X", "Y")
  proj4string(xy) <- CRS("+proj=longlat +datum=WGS84")  ## for example
  res <- spTransform(xy, CRS(paste("+proj=utm +zone=",zone," ellps=WGS84",sep='')))
  return(as.data.frame(res))
}

utm_coords <- LongLatToUTM(dat$lon, dat$lat, zone = 33)
#> Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
#> prefer_proj): Discarded datum Unknown based on WGS84 ellipsoid in CRS definition
dat$X <- utm_coords$X/1000 # for computational reasons
dat$Y <- utm_coords$Y/1000 # for computational reasons

Make Gifs

Save data

write.csv(dat, file = "data/for_analysis/catch_q_1_4.csv", row.names = FALSE)